Java Exception handling is a powerful feature of Java. Exception handling allows us to handle any exception that occurs at runtime and stop the normal flow of a program.
Examples of some common exceptions include NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, and ArithmeticException.
An unintended event that has affected the program’s execution at run or compile time. Exceptions can be controlled using try, catch and finally block.
In try block we write code which may throw an exception. If any exception occurs at particular line inside try block, the rest of the code after that line inside that try block will not get execute.
Catch will get call only at the time of exception occurs. If catch block is not able handle exception, it will throw this to caller function. Catching a wrong exception will also break the normal flow of a program.
Note: try block can be written without catch and finally block.
Syntax:
Common reasons for exceptions:
This is an abnormal condition which was never thought of and occurred unexpectedly to stop programme execution. System crash and out of memory are the best example of error.
Dev team should never try to handle errors.
It is a subclass of throwable.
There can be various reasons which cause our application to throw exception. For example, open a file which doesn’t exist, connecting to a server, accessing an array with no existence index, dividing any number by zero.
OUTPUT:
Explanation: In the above example an exception (java.lang.ArithmeticException) got occur at line number 5 on run time which broke our normal flow of program and because of that the line number 6 and 7 didn’t get execute.
OUTPUT:
Note: As we can see in above example, the normal flow of a program didn’t get break after handling an exception. For handling an exception, we covered line number 7 with try catch block.
Checked Exception (Compile time exception) -> The checked exception is thrown at compile time by the compiler.
Example:
OUTPUT:
NOTE: As mentioned the checked or compile time exception will get thrown at the time of code compilation.
2. Unchecked Exception (Runtime exception) -> The unchecked exception is thrown at run time while execution of program.
Example:
OUTPUT:
Finally block is used to execute for closing an open connections.
Note:
In the below example finally will called just after exception is thrown.
OUTPUT:
Throws keyword is provide us to throw an exception explicitly form a method, constructor or block of code.
OUTPUT:
Throws is a java keyword which indicate that this method can throw this type of exception and caller function need to catch the same exception.
Below function myfunc() will throw an Exception(ArithmeticException) which will get catch by caller main() function.
OUTPUT:
Custom Exception is all about creating a user defined exception derived from Exception class. The class needs to extend from the Exception class to create a custom exception and override one argument constructor for create a meaning message.
Below is an example of creating a custom exception.
OUTPUT:
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved