Exception Handling in Java

The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

Definition : An Exception represent an error condition that can occur during the normal course of program execution. When an exception occur the normal sequence of flow is terminated and the exception-handling routine is executed. When an exception occurs we say an exception is thrown ,When the matching  exception handling code is executed we say the thrown exception is caught.

In Other word we can say an exception handling is a mechanism to handle to run time error of program such as Arithmetic Error ,IO error etc.

When an exception occurs we say exception is “thrown” and handling exception is called  “caught” the exception.

Definition 2 : In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

Advantage of Exception Handling



The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario:

1.      statement 1;  
2.      statement 2;  
3.      statement 3;  
4.      statement 4;  
5.      statement 5//exception occurs  
6.      statement 6;  
7.      statement 7;  
8.      statement 8;  
9.      statement 9;  
10. statement 10;  
1.      statement 1;  
2.      statement 2;  
3.      statement 3;  
4.      statement 4;  
5.      statement 5;//exception occurs  
6.      statement 6;  
7.      statement 7;  
8.      statement 8;  
9.      statement 9;  
10. statement 10;  

Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the exception will be executed. That is why we use exception handling in java.

Types of Exception :
1.      Checked Exception
2.      Unchecked Exception

A checked Exception is an exception that is checked at compile time all other exceptions are unchecked exception also called Runtime exception.
Unchecked exceptions are not checked at compile-time rather they are checked at  Runtime , ArithmeticException, NumberformatException are two example of Runtime exception.

Some Example of Exception :

a.     ArithmethicException :
Int a=13/0;

b.     NumberFormatException :





** Understanding parseInt() method:

Instead of using the data type specific methods such as nextInt, nextDouble,
and others of the Scanner class, we can input a numerical value in a string format
and convert it to an appropriate data type by ourselves. For example, we can use the class method parseInt of the Integer class to convert a string to an int. Here’s a statement
In other word parseInt() takes numerical value as an integer and return integer.

that converts "123" to an int value 123:
int num = Integer.parseInt("123");

c.      NullPointerException :

If we have null value in any variable and if we try to perform any operation by the variable then it occurs a NullPointerException.



Getting Started In Exception :

try-catch Block :

try and catch block is used to handle exception. try block is used to enclose the code that might throw an exception. It must be used within the method.
Java catch block is used to handle the Exception. It must be used after the try block only. We can use multiple catch block with a single try.
If an exception occurs inside the try block then the statements remaining inside try block is skipped and the execution flow passed to the matching catch (handler block) block.



Here is a simple figure it might be helpful.


** keyword : throw and throws.

The “throw” keyword is used to throw an exception.
We can throw either checked or unchecked exception in java by throw keyword. The throw keyword is mainly used to throw custom exception.

Syntax of “throw” : throw < a throwable object>
Example : throw new myexception(“This is an Error.”);

As I mentioned before < a throwable object > which is an instance of Exception Class. When create an instance of the Exception class we can pass the String that describes the error, the thrown exception is caught by the corresponding catch block and the error message will be displayed.

Simple Code :

package exceptionhandeling;
import java.util.*;

public class NewClass3 {   
    public static void main(String args[]){       
        Scanner input=new Scanner(System.in);
        int age;          
                println("Enter your Age : ");
        
                try{
                     age=input.nextInt();                
                     if(age<=0){
                         throw new Exception("What the Hell you are Doing !!");
                     }
                     if(age>99)
                         throw new Exception("Amare Ki Mone hoy ?? ");             
                }
                catch(InputMismatchException myexception){
                    input.next();
                   
                    println("Please Enter Digit only.");
                }
                catch(Exception myexception){
                    println("Error : "+ myexception.getMessage());
                }
    } 
    public static void println(Object n){
        System.out.println(n);
    }
}

Enough for today. I will try to write more dipper in Exception Handling to my next article I hope you enjoyed it. May be I made some mistake please let me know about so that I can improve it.

Author : Md. Shohanur Rahaman

Date : 06-02-2015.

Download Pdf