0
enter code here
  `class Rethrow
   {
    public static void genException()
   {
        int n[]={4,8,16,32,64,128};
        int d[]={2,0,8,0,4};

        for(int i=0;i<n.length;i++)
        {                                     
            try{
                System.out.println("n/d is:"+n[i]/d[i]);

               }
            catch(ArithmeticException exc)
              {
                System.out.println("Cant Divide By Zero");
                throw exc;
              }
            catch(ArrayIndexOutOfBoundsException exc)
             {
                System.out.println("No match element found ");
                // rethrow the exception
             }
        }
    }
}

class RethrowDemo
{
    public static void main(String args[])
    {
        try 
        {
            Rethrow.genException();
        }
        catch(ArithmeticException exc)  // catch the rethrow Exception
        {
            // recatch exception
            System.out.println("Fatal Error "+"Program Termiated.");
        }
    }
}

this is output of program

Question 1::why does catch of "RethrowDemo" CLASS terminate an exception thrown by catch(Arithmetic Exception) of "Rethrow" class.

Question 2:: how does transfer of control working ??

Lalit kumar
  • 382
  • 4
  • 13
  • 3
    Please format your code. As it is I can't see the blocks correctly and thus can't help. – Thomas Jul 19 '16 at 12:49
  • Please share you error log for more information. – Janny Jul 19 '16 at 12:50
  • 4
    When you were asking your question, there was a big orange **How to Format** box to the right of the text area with useful information in it. There was also an entire toolbar of formatting aids. And a **[?]** button giving formatting help. *And* a preview area located between the text area and the Post Your Question button (so that you'd have to scroll past it to find the button, to encourage you to look at it) showing what your post would look like when posted. Making your post clear, and demonstrating that you took the time to do so, improves your chances of getting good answers. – T.J. Crowder Jul 19 '16 at 12:51
  • Questions are completely unclear. What do you mean by "**terminate** an exception"? Working through this will help you: https://docs.oracle.com/javase/tutorial/essential/exceptions/ – Fildor Jul 19 '16 at 12:55
  • Why does that make you wonder? An uncaught exception that happens inside a `try` block is caught, if there is a applicable catch block. Obviously this is the case here. – fabian Jul 19 '16 at 12:57

1 Answers1

1

In Java, when an event occurs that disrupts the normal flow of your application an Exception object is created and it is passed up the call stack to either be dealt with by the caller or passed further up to be dealt with/handled by something else higher up in the hierarchy.

Due to the fact you can't divide by zero an ArithmeticException is thrown from the line System.out.println("n/d is:"+n[i]/d[i]); and since you're doing this within a try...catch block, your catch(ArithmeticException exc) says "If there is an ArithmeticException thrown from within the try then I'm here to deal with it".

It is in this catch block you are printing out Cant Divide By Zero and then re-throwing the original exception. This then bubbles up to the calling method, in your case to the main method but since you are making the call from within a try...catch(ArithmeticException exc) that catch in main says "I will deal with that ArithmeticException that you have just re-thrown". It is at this point you then print Fatal Error Program Termiated and the application ends.

There are plenty of tutorials that will explain fully how exceptions work in Java so it would be useful to take a look at a few.

A Tutorial on Exceptions

Another Tutorial on Exceptions

kstandell
  • 775
  • 6
  • 16