3

When I am executing following code

1.    public class test {
2.      public static void main(String[] args) {
3.          try {
4.              int i = 10 / 0;
5.          } catch (Exception e) {
6.              int j = 10 / 0;
7.          } finally {
8.              int k = 10 / 0;
9.          }
10.     }
11.    }

I am getting an error:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at test.main(test.java:8)

I am not getting the reason why JVM is throwing exception from main() and not the catch block.

Ashima
  • 129
  • 1
  • 11
Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77

6 Answers6

2

If you follow the step-by-step description of a try-catch-finally in the JLS, you see that (in summary) if catch throws an exception then finally is executed and (emphasis mine):

If the catch block completes abruptly for reason R, then the finally block is executed.

If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).

So the exception thrown by the catch block (which is executed before the finally block) is discarded and the reported exception is the one thrown in the finally block.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
1

Because in every block - try/catch/finally you have a division by zero. Add another try-catch block inside catch and report an error in catch.

For example:

public class test {
    public static void main(String[] args) {
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            try {
                int j = 10 / 0;
            } catch (Exception e) {
                // report an error here - do not do any business logic
            }
        } finally {
            int k = 10 / 0;
        }
    }
}
daxur
  • 359
  • 1
  • 11
  • suppose in inner try...catch block again there is error than what to do ? – Yogesh Prajapati Jul 31 '13 at 10:25
  • Nothing - if you want to hide it, just have a blank catch block, but if you want to display it, then you can do that also. It's completelly up to you what you want to do with it. Just keep it simple - because if you get an exception there again - it will be thrown as normal uncatched exception. – daxur Jul 31 '13 at 10:28
1

One interesting thing to note is that, if you comment your code in the finally statement, the ArithmeticException thrown will concern the code in your catch statement.

I believe what's happening here is that the Exception that should be thrown in your catch statement is ignored, because an Exception is thrown in your finally statement.

Mena
  • 47,782
  • 11
  • 87
  • 106
1

The exception

in  } catch (Exception e) {
              int j = 10 / 0;
             }

will be thrown to finally block, if you remove finally block, you will got an

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at test.main(test.java:6)

All exception in catch block will be thrown to finally block also finally block will be executed any way

Gladiator
  • 264
  • 1
  • 5
1
try 
{
     System.out.println("Try block 1");
     int i = 10 / 0; //First Exception
     System.out.println("Try block 2");
 } catch (Exception e) { //Catched the first Exception
      System.out.println("Catch block 1");
      int j = 10 / 0; //Again Exception -->  Who will catch this one
      System.out.println("Catch block 2");
 } finally {
       System.out.println("finally block 1");
       int k = 10 / 0; //Again Exception -->  Who will catch this one
       System.out.println("finally block 2");
 }

If an exception occurs within the try block, that exception is handled by an exception handler associated with it. To associate an exception handler with a try block, you must put a catch block after it.

You associate exception handlers with a try block by providing one or more catch blocks directly after the try block.

Each catch block is an exception handler and handles the type of exception indicated by its argument

The finally block always executes when the try block exits.

Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

In Java Exception Handling, no matter the exception is caught or not, but if you have used finally block, it would always get executed.

Hence, what you are doing in your code is-

  1. Throwing exception object from main()
  2. Catching exception in catch block
  3. Throwing exception caught in catch block to the finally block (it would get executed anyway!)

In this code, either you use only catch or only finally, to get proper result.

and, since you want exception to be caught in catch, omit finally, just use catch.

Good luck!

Ashima
  • 129
  • 1
  • 11