Exception Handling

Exception Handling

an exception is abnormal condition that arises in a code sequence at runtime or it’s a run time error.

Throwable

Exception

Error

RuntimeException

 

 

Throwable is the superclass for both exception and error class.

The exception class is again classified as the checked and unchecked exception

The checked exception are not derived from the RunTimeException but derived from the throwable class and these are checked at compile time

Ex-IOException,ClassNotFoundException etc

The unchecked exception are derived from the RunTimeException and are not checked at compile time

Ex-Arithmaticexception,NullPointerException etc

The exception can be generated by the java run time system or can be generated manually by the progarmmer

The exception handling in the java is handled by the  five key words i.e try,catch,throw,throws and finally

Statements that we want to monitor for exception are put in the try block and the exceptions are thrown from the try block should be caught by the catch block

To manually throw an exception  we use the keyword throw .Any exception that is thrown out of a method must be specified as such by a throws keyword

Any code that we want to execute after the try block will be written in the finally block

In java, if a program does not have any exception handlers then, the exception which are occurred during the execution will be taken care by the default exception handler. It’s The good practice have an exception handler in the program apart from the default exception handler

Simple Example on the usage of try and catch

public class Jan301 {

    void fun()
    {
        try
        {
            int b=2/0;
        }
        catch(ArithmaticException c)// if we provided as (Exception c)then all type 
        {                                                     //of exception will be caught here      
            System.out.println("exception of method");
        }
    
    
    public static void main(String[] args) {
        
        Jan301 j=new Jan301();
        j.fun();
        
        System.out.println("exception handinling");
    }    
}

Multiple exception can be caught under single catch block using | operator

Ex-

class Jan301{
    public static void main(String[] args) {
        
        try
        {
        int a[]=new int[10];
        int b=10;
        
            System.out.println(a[11]);
        
            
            String s=null;       //nullpointerexception
            System.out.println(s.length());
            
            String p="sdm";     //numberformatexception
            int d=Integer.parseInt(p);
        }s
        catch(ArithmeticException | ArrayIndexOutOfBoundsException |NullPointerException |NumberFormatException p)
        {    
            
            System.out.println(p);
        }
    }
}

Exceptions which are defined by the user are called user defined exception or the custom exception
Ex-
class cantwithdraw extends Exception// Exception is base class for all the user defined 
{                                                                    //exception so that’s why we are extending base class
    cantwithdraw(String x)
    {
        System.out.println(x);   
    }
}


public class exception {
    
    int amt;
    
    void deposit(int amount)
    {
        amt=amount;
        System.out.println("total amount"+" "+amt);
    }
    
    void withdraw(int amount)
    {
        try
        {
            if(amount<500)
               throw new cantwithdraw("unable to with draw");//
             else
                amt=amt-amount;
            System.out.println("balence amount is"+" "+amt);
            
        }
        catch(cantwithdraw a)//user defined exception or the custom exception
            
        {
            System.out.println("cant withdraw the amount");
        }
    }
    public static void main(String[] args)
    {
        eption e=new eption();
        e.deposit(10000);
        e.withdraw(400);
    }
    
}

Posted on by