Exception Handling,throw,throws

try block: Exception Possible Area catch Block: Exception Handling Area finally Block: Code Cleansing Area 1) try block inside another try - Nested try try { int no1 = 10, no2 = 5; System.out.println(no1/no2); try { int[] ar = new int[no1]; } catch(NegativeArraySizeException na) { } } catch(ArithmeticException ae) { } 2) try block inside catch block. int no1 = 10, no2 = 0; try { System.out.println(no1/no2); } catch(ArithmeticException ae) { Scanner sc = new Scanner(System.in); no2 = sc.nextInt(); try { System.out.println(no1/no2); } catch(ArithmeticException aa) { } } 3) try catch block inside finally block Yes, it is possible. throws: We can add throws after method signature. We can add any number of throws after method signature. If throws is given, try catch can / should be added at Method calling statement. public class RecursionDemo { public static void main(String[] args) { RecursionDemo rd = new RecursionDemo(); rd.divide(100, 20); } public void divide(int no1, int no2) throws ArithmeticException, ArrayIndexOutOfBoundsException, NegativeArraySizeException { System.out.println(no1/no2); int[] ar = new int[no2]; for(int i=0; i

Mar 18, 2025 - 13:37
 0
Exception Handling,throw,throws

try block: Exception Possible Area
catch Block: Exception Handling Area
finally Block: Code Cleansing Area

1) try block inside another try - Nested try


        try
        {
            int no1 = 10, no2 = 5; 
            System.out.println(no1/no2);
            try
            {
                int[] ar = new int[no1]; 
            }
            catch(NegativeArraySizeException na)
            {

            }
        }
        catch(ArithmeticException ae) { }

2) try block inside catch block.

int no1 = 10, no2 = 0; 
        try
        {

            System.out.println(no1/no2);
        }
        catch(ArithmeticException ae)
        {

            Scanner sc = new Scanner(System.in); 
             no2 = sc.nextInt(); 
             try {
            System.out.println(no1/no2);
             }
             catch(ArithmeticException aa)
             {

             }
        }

3) try catch block inside finally block
Yes, it is possible.

throws:
We can add throws after method signature. We can add any number of throws after method signature. If throws is given, try catch can / should be added at Method calling statement.

public class RecursionDemo
{
    public static void main(String[] args) 
    {
        RecursionDemo rd = new RecursionDemo(); 
        rd.divide(100, 20);
    }
    public void divide(int no1, int no2)
    throws ArithmeticException, ArrayIndexOutOfBoundsException,
    NegativeArraySizeException
    {
        System.out.println(no1/no2);
        int[] ar = new int[no2]; 
        for(int i=0; i<10; i++)
            System.out.println(ar[i]);
    }
}
public class Patterns {

    public static void main(String[] args) {

        RecursionDemo rd = new RecursionDemo();
        try 
        {
        rd.divide(0, 0);
        }
        catch(ArithmeticException ae)
        {
            System.out.println("Check no2");
        }

    }
}

throw:
Predefined Exceptions.
User Defined Exceptions.
throw is mainly used for User Defined Exceptions. We can add throw inside method definition. After 'throw' keyword,we should mention Exception object.

package pratice;

public class Exeception_throw extends RuntimeException {
    public void checkPassword(String pwd) {
        if (pwd.length() < 8) {
            Exeception_throw pe = new Exeception_throw();
            throw pe; // throw may used for user define object//throw object
        }

    }

}
package pratice;

import java.util.Scanner;

public class public_login {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Password: ");
        String password = sc.next();
        Exeception_throw pp = new Exeception_throw();
        pp.checkPassword(password);

    }

}

Enter Password:
123456
Exception in thread "main" pratice.Exeception_throw
at Second/pratice.Exeception_throw.checkPassword(Exeception_throw.java:6)
at Second/pratice.public_login.main(public_login.java:12)