Cuban coffee
Cuban coffee

Reputation: 11

throw Exception java

I got below code but I do not understand why it prints B. Also Can you explain?:

throw Math.random() > 0.5 ?new MyException(): new RuntimeException();

code:

public class MyException extends RuntimeException {

}


public class Test {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
        method1();
    }catch(MyException ne){
        System.out.print("A");
    }

  }

  public static void method1() {
    // TODO Auto-generated method stub
    try{
        throw Math.random() > 0.5 ?new MyException(): new  RuntimeException(); 
    } catch (RuntimeException re){
        System.out.print("B");
    }
  }
}

Thank you!!

Upvotes: 0

Views: 1432

Answers (3)

Sarath Chandra
Sarath Chandra

Reputation: 1878

To begin with,

I'd like to point out, for the sake of clarity, that the piece of code

throw Math.random() > 0.5 ?new MyException(): new RuntimeException(); is what is called as an inline form of the if-statement, written using the ternary operator ?.

So, essentially,

throw Math.random() > 0.5 ?new MyException(): new RuntimeException();

is the same as

if(Math.random() > 0.5){
  throw new MyException();
} else{
  throw new RuntimeException();
}

With that out of the way, let's focus on the flow of the program:-

  1. The main method calls method1 in a try block.
  2. The invocation of method1 opens its own try block.
  3. The invocation of Math.random() returns a decimal value between 0 and 1; each invocation returning a value most possibly different than the one in the previous invocation.
  4. So, a MyException object is thrown when the Math.random() invocation returns a value greater than 0.5. Else, a RuntimeException object is thrown.
  5. As the MyException class extends RuntimeException, in both cases of the random value being greater than 0.5 or not, the catch clause can handle both the exceptions. This is because the catch clause can catch all instances of RuntimeException, as well as all its sub-types.

Reference from the official Java tutorials available here:

The system considers it a match if the thrown object can legally be assigned to the exception handler's argument.

  1. The Exception object thrown is caught in the catch block of the method1 invocation. And this prints "B".
  2. The control of execution terminates at the end of this catch block and the program flow never reaches the calling method, the main method in this case. Hence, the "A" is never printed.

I hope this clarifies the various points involved.

Upvotes: 1

Prim
Prim

Reputation: 2968

In method1(), you catch all RuntimeException and print "B". On the other hand, MyException extends RuntimeException in your code.

So, all MyException or RuntimeException are catched by the catch block and print 'B'

You have 'A' never printed because the catch block in method1 handle the exception by catching it. You can rethrow the exception to main method by adding at the end of catch block of method1 the statement throw re;

Upvotes: 0

Eran
Eran

Reputation: 393771

method1 throws either MyException or RuntimeException depending on whether the random number returned by Math.random() is higher than 0.5 or not.

It always prints B, since regardless of whether MyException or RuntimeException was thrown, both will be caught by the catch block that catches RuntimeException and prints B (since MyException is a sub-class of RuntimeException).

Upvotes: 0

Related Questions