DEV Community

tejaswini-59
tejaswini-59

Posted on

JAVA EXCEPTIONS

If a Java program encounters an error , normally it will stop and generate an error message . The technical term of this is called an Exception . It is normal that a programmer make coding errors . Exceptions disrupts the normal flow of a program .

TYPES OF EXCEPTIONS:
1 . Checked exception
2 . Unchecked exception
3 . Error

EXCEPTION HANDLING
It is the process of handling runtime exceptions such as ArrayIndexOutOfBound , IOException , ArithmeticException etc .

EXCEPTION KEYWORDS
1 . try-catch block :
syntax: try{
}catch(Exception_name ref){}

2 . try-finally block :
syntax : try{
}finally{}

3 . throw :
syntax : throw exception;

4 . throws :
syntax : return_type method() throws Exception_name

EXAMPLE PROGRAM :
class Main{
public static void main(Strin args[]){
try{
int a =5/0;}
catch(ArithmeticException e){
System.out,println("ArithmeticException--" + e.getMessage());}
finally{
System.out.println("This is Finally block");}
}}

OUTPUT : ArithmeticException -- /by zero
This is Finally block

EXPLANATION :In the above program , a number is divided by '0' , so it gives ArithemticException(divide by zero) , which is caught by catch block the followed by finally block

Top comments (0)