1

This might be a really dumb question to most of you here, really sorry about that. I am new to java and the book i am reading didn't explain the working of an example in it.

public class CrazyWithZeros
{
public static void main(String[] args)
{
    try
    {
    int answer = divideTheseNumbers(5, 0);
    }
    catch (Exception e)
    {
        System.out.println("Tried twice, "
        + "still didn't work!");
    }
}

public static int divideTheseNumbers(int a, int b) throws Exception
{
    int c;
    try
    {
        c = a / b;
        System.out.println("It worked!");
    }
    catch (Exception e)
    {
        System.out.println("Didn't work the first time.");
        c = a / b;
        System.out.println("It worked the second time!");
    }
    finally
    {
        System.out.println("Better clean up my mess.");
    }

    System.out.println("It worked after all.");
    return c;
}

}

I can't figure out where the control will go after another exception is generated in catch block in divideTheseNumbers() method ? Any help will be appreciated !

Razib
  • 10,965
  • 11
  • 53
  • 80
Tejas K
  • 682
  • 11
  • 28
  • It will not be caught, the exception will be thrown to the method that called `divideTheseNumbers`. – Karthik Jul 26 '15 at 18:37
  • The `divideTheseNumbers()` method throws an exception then whee the catch block inside the method? – Razib Jul 26 '15 at 18:37
  • @karthik - so the output should be - Didn;t work for the first time then the statement below it will throw an exception to main method, which will be caught in catch and it will display Tried twice, still didn't work. Right ? – Tejas K Jul 26 '15 at 18:41
  • @TejasK I added an answer explaining the flow. Let mw know if you don't understand any part of it. – Karthik Jul 26 '15 at 18:49

2 Answers2

1

Output for your program will be

    Didn't work the first time.
    Better clean up my mess.
    Tried twice, still didn't work!

Didn't work the first time. - because of the catch block in divideTheseNumbers

Better clean up my mess. - because of the finally block in divideTheseNumbers

Tried twice, still didn't work! - because of the catch block in the main method.

In general when a exception is thrown from a method and if is not in a try block, it will be thrown to it's calling method.

There are two points to note :

1) Any exception that is not in a try block will be just thrown to it's calling method(even if it is in a catch block)

2) finally block is always executed.

In your case, you are getting the second exception in catch block, so it will be thrown. But before exiting any method it will also execute finally block(finally block is always executed). That is why Better clean up my mess is also printed.

Karthik
  • 4,950
  • 6
  • 35
  • 65
1

Some points regarding exception handling:

1. Place your code that may causes exception inside the try{} block.
2. Catch the appropriate exception using the catch block.
3. While catching exception it's better use more specific type exception instead of catching the generic exception like. For example you catch the Exception, in this case you may catch the more specific type exception ArithmeticException.
4. finally block always executed, even though you use return statement in catch or try block.
5. A method either throws or catch an exception. If a method catch an exception then for it is not required to throws the exception.
6. All exception need not to be handled by using catch-block. We can avoid the try-catch block for the unchecked exception like - ArithmeticException,NullPointerException, ArrayIndexOutOfBoundsException. See here for more.

You may also check the tutorial for learning more about exception handling.

Community
  • 1
  • 1
Razib
  • 10,965
  • 11
  • 53
  • 80