0

I have simple program with throwing exceptions, and I understand output I get, but not exception I get. Here is the code:

//Q1.java
import java.io.*;
import java.util.*;

public class Q1{
  public static void main(String args[]){
    Q1 q1=new Q1();
    Q2 q2=new Q2();
    try{
      System.out.println(q2.method(q1));
      System.out.println(q2.method(q2));
    }catch(QE1 e){
      System.out.println("exception 1");
    }finally{
      System.out.println("finally");
    }
  }

  Object method(Q1 q) throws QE1{ 
    if(q instanceof Q1){
      System.out.println("method");
    }
    else {
      throw new QE2();
    }
    return 1;
  }
}

class Q2 extends Q1{

  Object method(Q1 q) throws QE1{
    if(errorCheck()&& q instanceof Q2){
      throw new QE2("error 2");
    }else if(q instanceof Q2){
      throw new QE1();
    }else{
      return new String("abc");
    }
  }

  boolean errorCheck(){
    return true; 
  }

}

class QE1 extends Throwable{
  public QE1(){
    System.out.println("qe1 - 1");
  }
  public QE1(String s){
    super(s);
    System.out.println("qe1 - 2");
  }
}

class QE2 extends RuntimeException {

  public QE2(){
    System.out.println("qe2 - 1");
  }
  public QE2(String s){
    this();
    System.out.println("qe2 - 2");
  }

}

Output is:

abc
qe2 - 1
qe2 - 2
finally
QE2
    at Q2.method(Q1.java:33)
    at Q1.main(Q1.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

and then program breaks, but I am not sure why.
Problematic line is the one in constructor of QE2.
I kind of assume that, because QE2 is subclass of RuntimeException and for that reason, constructor of RuntimeException is called, so program breaks.
Is this the case or something else?

6
  • What do you mean with "breaks". Please add the stack trace you get! You see, this code is probably on purpose written to be hard to read. Do you really think this is good input for other people? You making it 10 times harder for us to help you; just because of this over-complicated code in there. Commented Oct 17, 2016 at 18:41
  • do you get a stacktrace at the command line? if so please show. Commented Oct 17, 2016 at 18:41
  • 1
    Also it might be helpful to give your class and method names something meaningful. Calling it "Q1" and "Q2" and "method" will get you confused very quickly. You can do what you want but this is bad practice imo. Commented Oct 17, 2016 at 18:42
  • Calm down everyone. I usually don't write code like this, confusing and uninformative. I am practicing for exam, and it should be confusing. Also, I added stack trace. Commented Oct 17, 2016 at 18:48
  • 1
    @misty I gave you an answer below. There is nothing unexpected happening in your code, as far as I can tell. Commented Oct 17, 2016 at 19:03

1 Answer 1

1

There is nothing unexpected about the output/behavior of your code.

public static void main(String args[]) {
    Q1 q1 = new Q1();
    Q2 q2 = new Q2();
    try {
        // this prints "abc" and no exception is thrown
        System.out.println(q2.method(q1));

        // this throws a new QE2(), which prints
        // this prints "qe2 - 1" followed by "qe2 - 2"
        System.out.println(q2.method(q2));
    } catch(QE1 e) {
        System.out.println("exception 1");
    } finally {
        // your code ends up here, which prints "finally"
        System.out.println("finally");
    }
}

You are throwing an exception of type QE2 in the try block of your main method. However, you never actually catch that type of exception, since you only catch QE1. As a result, you end up in the finally block, which prints "finally".

If you want to catch all exceptions, then you can use catch (Exception e) right before the finally block.

Sign up to request clarification or add additional context in comments.

6 Comments

I understand what program prints. But why do I get exception?
You throw an exception of type QE2, but you do not catch it. What do you think throwing an exception means?
Yeah, I get it know. Thank you very much! One thing left confusing is this: shouldn't catch block with QE1 catch all exceptions, since it is subclass of Throwable?
@misty QE1 is a subclass of Throwable, but not a subclass of Exception, so this is why QE2, is not caught.
It is confusing for me because if there was catch(Throwable e), QE2 would be caught...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.