Hello friends, today we learn about the concept of Java EXCEPTIONAL HANDLING or ERROR HANDLING. When any code is written then there is always a chance of arising any exception or error which may interrupt the processes of execution of program which is undesirable. To preserve the process of execution of program we should handle the exception with a use of different type of block like try, Catch and Throw.
Exceptional Handing in java or any programming language is very important concept which is use achieve or preserve the processes of execution of Program. There are different type of block which is used to handle the exception are listed below.
Try block is used at that section of block in which the probability of exception is higher for example:
In that code when value of y=0. Then exception will occurred.
Throw is a function which is used to throw exception which is generated in try block and it throw exception toward catch block.
Catch block is used to catch exception which is thrown from try block by using throw function.
Finally block executed after the execution of catch block.it is used to give final action to code for example:
Now Simple code Used to Handle Exception in Java,
public static void main(String args)
Scanner sc=new Scanner(System.in);
System.out.println(“Simple Java Exceptional Handling Programme By Buffercode”);
System.out.println(“Enter Two Number Which You Want to Divide”);
System.out.println(“Result is :”+z);
System.out.println(“Exception is =”+ae);
System.out.println(“Java Exception Handle”);
HOW TO COMPILE THE CODE:
1) Save following code in notepad as excep_handl.java
2) Open command prompt and go to file location by cd command for example:
3) Compile program by using javac command for example: javac execp_handl.java
4) Run program by using java command for example : java excep_handl
5) After that your output will come.
It’s So Easy…………………………………
Thanx for reading the post………by Praveen Tiwari