Exception Handling

Exception Handling in Java programming language

Exception Handling in Java programming language

We will discuss in this tutorial about Exception Handling in Java programming language

The Java Exception handling is the most useful mechanism for the programmer to handle the exception. the runtime error caused by Java exception.

What is exception

The exception is one of an abnormal condition of the unwanted situation or unexpected result that occur during the execution time(It is not an error)it is interrupted the normal flow of control in the program. When the exception appear the program goes to terminates. Sometimes we get an error message from the system. But Java contains exception handling system when handling the exception we can offer a meaningful message according to the exception and understandably

Exception Handling

Advantages of exception handling

Java exception handling is used to maintain normal flow control of the application in program

When the exception can occur

An exception can occur two situations

  1. runtime exceptions
  2. compile time exception

Basically, Java exception is Java objects and throws them

An exception can be handled by try and catch block

Error

An error is thrown y the Java runtime system and identifies some irrecoverable condition that occurs during the program execution

Different between exception and errors

Exceptions can be handled by the user or programmer but we can not handle the Error by user or programmer

try and catch block

try{
//some code here
}catch(Exception type 1 Ex_obj){
//some code here
}
finally
{
//some code here
)

What type of exception occurs

Checked Exception – the checked exception is checked compile time

Eg – IO Exception, SQL Exception

Unchecked Exception – unchecked exception is not checked at compile time- They are checked runtime

Ex – Arithmetic Exception,  Null Pointer Exception,  Array Index Out Of Bound Exception

 

Hierarchy of Java Exception classes

The hierarchy of Java exception classes given below

Hierarchy of Java Exception Handling

 

We can use five handlers (keywords)  in Exception Handling in Java language.

  1. try block –
  2. catch block –
  3. throw block –
  4. throws block –
  5. finally block

The code might throw some exception  should be kept in the try block

The catch block can have the code to handle the exception or log the same

finally block can be used to clean up code or release some resource that is utilized in the program

 

Types of Exceptions in Java language

  1. Arithmetic Exception
  2. Class is not found Exception
  3. illegal Argument Exception
  4. Index out of bound Exception
  5. IO Exception

 

Arithmetic Exception
class ExceptionOne{
public static void main(String args[]){
int x=0;
int y=45/x;
System.out.println("Answer is"+y);

}
}

 

When the above code is executed, it produces the following results:

Output

In this program, the Java runtime system detects an exception as divide by zero and generate default message by Java runtime system

How to handle an arithmetic exception in Java

We provide exception inside the try block. try block detect the exception and throw them to catch block

class ExceptionOne{
public static void main(String args[]){
int x=0;
try{
int y=45/x;
System.out.println("Answer is"+y);
}catch(ArithmeticException e){
    System.out.println("This is arithmetic exception\n Division by zero");
}
}
}

 

When the above code is executed, it produces the following results:

Output
Index Out Of Bound Exception
class ExceptionTwo{
public static void main(String args[]){
int marks[]={45,34,76,87,89};
marks[5]=70;
}
}

When the above code is executed, it produces the following results:

Output

 

How to handle ArrayIndexOutOfBoundException  in Java

class ExceptionTwo{
public static void main(String args[]){
    try{
int marks[]={45,34,76,87,89};
marks[5]=70;
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println("This is Array Index Out Of Bounds Exception"+e);
    }
}
}

When the above code is executed, it produces the following results:

Output

 

Example

class ExceptionThree{
public static void main(String args[]){
    try{
        int x=0;
        int y=45/x;
int marks[]={45,34,76,87,89};
marks[5]=70;
    }catch(ArithmeticException e){
        System.out.println("This is ArithmeticException\n"+e);
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println("This is Array Index Out Of Bounds Exception\n"+e);
    }finally{
        System.out.println("I am always executed");
    }
    System.out.println("Execution completes");
}
}

When the above code is executed, it produces the following results:

Output

Suggested for you

try keyword in Java

Catch keyword in Java

throw keyword in Java

throws keyword in Java

Finally keyword in Java

 

You might like also

Encapsulation in Java

The catch keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Share
Published by
Karmehavannan

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago