How can you handle the Exception in Java?explain with example?



Exception handling is managed by five keywords:
1. Try and Catch :- Try and catch are two different block. There is no statement between try and catch block. Try block and catch block are closed separately.One try block can be followed no of catch blocks.
2. Finally: It is a block. This block is executed either there is an exception or not. It is a type of destructor used to closed resources.
3. Throws :- It is a keyword, it is an inbuilt exception it is used to through the exception .
4. Throw :- Throw is a keyword, it is used to make user defined exception.
Example :-
class exception
{
public static void main(String args[])
{
int a = 10, int b = 5, int c = 5;
int x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException obj)
{
System.out.println("Division by zero");
}
y=a/(b+c);
System.out.println("y="+y);
}
}

Comments

Post a Comment

Popular posts from this blog

Write a program to add two number using inline function in C++?

Traversing of elements program with algorithm and Flowchart