DEV Community

Cover image for Exception Handling,throw,throws
Neelakandan R
Neelakandan R

Posted on

1 1 1 1 1

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) { }
Enter fullscreen mode Exit fullscreen mode

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)
             {

             }
        }
Enter fullscreen mode Exit fullscreen mode

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]);
    }
}
Enter fullscreen mode Exit fullscreen mode
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");
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

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
        }

    }

}
Enter fullscreen mode Exit fullscreen mode
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 fullscreen mode Exit fullscreen mode

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)

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong ยท web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video ๐ŸŒถ๏ธ๐Ÿ”ฅ

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

DEV shines when you're signed in, unlocking a customized experience with features like dark mode!

Okay