Exception Handling in Java programming language
- Home
- Exception Handling
- Exception Handling in Java programming language
- On
- By
- 0 Comment
- Categories: 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
- runtime exceptions
- 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
We can use five handlers (keywords) in Exception Handling in Java language.
- try block –
- catch block –
- throw block –
- throws block –
- 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
- Arithmetic Exception
- Class is not found Exception
- illegal Argument Exception
- Index out of bound Exception
- 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:
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:
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:
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:
Suggested for you
throw keyword in Java
throws keyword in Java