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.
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
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
Basically, Java exception is Java objects and throws them
An exception can be handled by try and catch block
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 )
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
The hierarchy of Java exception classes given below
We can use five handlers (keywords) in Exception Handling in Java language.
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
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:
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:
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:
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:
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:
throw keyword in Java
throws keyword in Java
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…