java program on exception handling(try or catch)

in this page we are learn about exception handling in java.
exception handling:-
an exception is a event which is an excepted and unwanted.it happen during execution of program.that disrupts the normal flow of the programming instruction.
the exception handling is one of the powerful mechanism which is use to handle runtime error it helps to maintain the normal flow of application. 

error vs exception:-
error:- an error indicate serious problem that a reasonable.application should not try to catch. 
exception:- exception indicate condition that reasonable application might try to catch.

try and catch :-
the try statement is used to testing a block code for error while it is being executed.
in the catch statement it allow a block of code to be execute if the error occurs in the try block.
the try and catch keyword comes in pare.

syntax:-
try{
// block of code to try
}
catch(execution e){
// block of code to handle error
}

//java program to execute exception handling.

class err

 

{

 

public static void main(String[] args) { int a=25,b=0,c=0;

 

try

 

{

c=a/b;

 

}

 

catch(ArithmeticException e)

 

{

 

System.out.println("Division by 0 Error."); System.out.println("Type of error:"+e);

 

}

 

System.out.println("The value of a is"+a);

System.out.println("The value of b is"+b);

 

System.out.println("The value of c is"+c);

 

}

 

}

 

Output:-

java program on exception handling(try or catch)

Post a Comment

0 Comments