******************************Exceptional Handling******************************
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.
1) Try
2) Catch
3) Throw
4) Finally
Try block is used at that section of block in which the probability of exception is higher for example:
try {
z=x/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 Java 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(“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:
cd e:\java
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…………………………………
****************OUTPUT*************
Further links for more programming concept
1) For programming click here.
2) For Query please register with us and comment on particular post.
Thanx for reading the post………by Praveen Tiwari