![]() |
![]() |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
An Exception is a condition that is caused by a run-time error in a program. We can also say that an Exception is an event which disturbs the normal flow of the program.
The Exception handling mechanism actually detects and reports an "Exception circumstance" so that the corrective action will be taken.
Sr.no | Task | Action |
---|---|---|
1. | Finding the problem in code | Hit the exception |
2. | Notify that an error has occurred. | Throw the exception |
3. | Collect the error information | Catch the exception |
4. | Take the proper action | Handling the exception |
Java exception handling is performed by following 5 keywords.
All program statements which are going to generate an exception are written inside the try block.
If an exception is found in the try block then it is thrown to particular matching catch block for handling purpose.
If any one statement generates the exception in try block then all other remaining statements are skipped and execution is jumped into the matching catch block.
Example
try { int a=25, b=0; int c = a/b; // generates an exception as Divided by zero. System.out.println("Division is : "+c); // compiler skipes this line and jumps into catch block. }
Catch block is responsible for handling an exception which is generated in the try block. It also contains multiple statements to take corrective action on the exception.
Every try block must have atleast 1 catch block, otherwise compiler gives an error.
try { int a=25, b=0; int c = a/b; // generates an exception as Divided by zero. System.out.println("Division is : "+c); // executed if there is no Exception. } catch(ArithmeticException AE) { System.out.println("Divided by zero error\nPlease change inputs"); // take action if error occurred. But never goes to try block. }
Throw keyword is used for manually (explicitly) throwing an exception.
Since throw is used for manually throwing an exception it is used within the method.
It doesn't allows us to throw multiple exceptions at a time. We have to go for the throws keyword.
try { throw new NullPointerException ("Null Exception"); } catch(NullPointerException e) { System.out.println("Null pointer exception caught"); }
Throws keyword allows us to execute multiple exceptions from a method.
If a method is generating an exception bu cannot handle it, then it is the job of method to specify the task to protect them from an Exception.
The exception that can be generated by method must be thrown by using throws clause otherwise the compile time error will be generated.
class ThrowsTest { static void throwsMethod() throws ArithmeticException { int a = 10, b = 0 , c; c = a/b; throw new ArithmeticException ("Error"); } public static void main(String args[]) { try { throwsMethod(); } catch(ArithmeticException e) { System.out.println("Divided by zero error"); } } }
If the exception is not caught by any catch block then finally block is responsible for handling the exception.
finally block statements are always executed after the try catch block has completed its execution.
Rules for writting finally block
Example
try { int a=25, b=0; int c = a/b; // generates an exception as Divided by zero. System.out.println("Division is : "+c); // executed if there is no Exception. } catch(ArithmeticException AE) { System.out.println("Divided by zero error. Please change inputs"); // take action if error occurred. But never goes to try block. } finally { System.out.println("Finally block executed"); }