In this tutorial, we will learn about the Exception Handling in Java. How to use them with the help of examples.
Exception Handling in Java
Exception handling is a process to trap the logical errors and manage it within program without terminating the flow of execution.
Error in Java
Error is the mistake happen in computer that produce irrelevant activities during compile or run time. The action on things that not fulfill the result is know as error.
There are two types of Error
- Compile time error
- Run time Error & Logical error
Compile time error
Compile time error happened during compilation of program due to typing mistake like leaving semicolon, writing wrong spell, etc. This error can be trap by the compiler during compile time and appropriate message. If program not successfully complied then it is not enable to run.
Run time Error & Logical error
Run time Error is the error occurs during running of program due to logical mistakes, means the result show on the screen is not appropriate as you required.
These are four basic action perform during exception handling.
- Hit – observe that there is error.
- Throw – returns that error.
- Catch – receive that error.
- Handle or manage – provide action on error.
How can we manage Exception in program?
Java provides a special block called try catch block to manage exception. Using the try— catch block, try can be written once but catch block can be one or more than one
Try block
Try {
- All the basic operation come inside try block.
- It is use Hit and Throw block.
}
Catch block
Catch (exception obj) {
- Operation related with logic error.
- It is use Catch and Handle block.
}
Syntax
The Syntax of Java try-catchtry{ //code that may throw an exception }catch(Exception_class_Name ref){}
Syntax
The Syntax of try-finally blocktry{ //code that may throw an exception }finally{}
Final Block in Java
This is also an optional block of try block. This is use with try block for executing the statements whenever exception is generate by try block or not. It means it is sure that finally block will be execute at any how whenever exception is generate or not or corresponding exception is define or not with catch block.
The finally block can be written at last after catch block or it can be written just after try block when catch is not define.
Example
Example of Exception Handlingimport java.io.DataInputStream; import java.io.IOException; public class Errorm { public static void main (String args[]) { DataInputStream z= new DataInputStream(System.in); int n[]= new int [4]; int i; try { System.out.println("Enter" +n.length+ "number"); for (i=0;i<n.length;i++) n[i]= Integer.parseInt(z.readLine()); for (i=0;i<n.length;i++) System.out.println(n[i]); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Bad Index" +e.getMessage()); } catch(IOException e) { System.out.println(e.getMessage()); } } }
Let’s run the code
Output Value
If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.
To know more about JAVA Wikipedia please click here .
Stay Connected Stay Safe, Thank you
0 Comments