******************************Exceptional Handling*****************
************* Hello friends, today we talk about the concept of 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 Catch Throw Finally Try block is used at that section of block in which the probability of exception is higher for example:
try {
z=ex/y ;
}
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.
catch(ArithmeticException ae) {
System.out.println(ae);
}
Finally block executed after the execution of catch block.it is used to give final action to code for
example:
finally {
System.out.println(“Exception Handled”);
}
Now Simple code Used to Handle Exception in Java, CODES:
import java.util.*;
class excep_handl {
public static void main(String args[]) {
int x,y,z; Scanner sc=new Scanner(System.in);
System.out.println(“Simple Exceptional Handling Programme By Buffercode”);
System.out.println(“Enter Two Number Which You Want to Divide”);
x=sc.nextInt(); y=sc.nextInt(); try { z=x/y; System.out.println(“Result is :”+z);
}
catch(ArithmeticException ae) {
System.out.println(“Exception is =”+ae);
}
finally {
System.out.println(“Exception Handle”);
}
}
}
HOW TO COMPILE THE CODE: Save following code in notepad as excep_handl.java Open command prompt and go to file location by cd command for example: cd e:java Compile program by using javac command for example: javac execp_handl.java Run program by using java command for example :
javac excep_handle.java
java excep_handle
After that your output will come. It’s So Easy enjoy programming…………………………………