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/no…


This content originally appeared on DEV Community and was authored by Neelakandan R

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)


This content originally appeared on DEV Community and was authored by Neelakandan R


Print Share Comment Cite Upload Translate Updates
APA

Neelakandan R | Sciencx (2025-03-18T11:49:43+00:00) Exception Handling,throw,throws. Retrieved from https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/

MLA
" » Exception Handling,throw,throws." Neelakandan R | Sciencx - Tuesday March 18, 2025, https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/
HARVARD
Neelakandan R | Sciencx Tuesday March 18, 2025 » Exception Handling,throw,throws., viewed ,<https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/>
VANCOUVER
Neelakandan R | Sciencx - » Exception Handling,throw,throws. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/
CHICAGO
" » Exception Handling,throw,throws." Neelakandan R | Sciencx - Accessed . https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/
IEEE
" » Exception Handling,throw,throws." Neelakandan R | Sciencx [Online]. Available: https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/. [Accessed: ]
rf:citation
» Exception Handling,throw,throws | Neelakandan R | Sciencx | https://www.scien.cx/2025/03/18/exception-handlingthrowthrows/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.