java - 简单的Java程序在抛出异常时中断

标签 java exception runtimeexception

我有一个简单的程序,可以抛出异常,并且我理解得到的输出,但不理解得到的异常。 这是代码:

//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");
  }

}

输出为:

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)

然后程序中断,但我不知道为什么。
有问题的行是 QE2 构造函数中的行。
我有点假设,因为 QE2 是 RuntimeException 的子类,因此,调用 RuntimeException 的构造函数,因此程序中断。
是这样的情况还是其他什么情况?

最佳答案

代码的输出/行为没有任何意外。

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");
    }
}

您在 main 方法的 try block 中抛出 QE2 类型的异常。但是,您永远不会真正捕获该类型的异常,因为您只捕获QE1。结果,您最终进入 finally block ,该 block 打印 "finally"

如果您想捕获所有异常,则可以在 finally block 之前使用 catch (Exception e)

关于java - 简单的Java程序在抛出异常时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40093401/

相关文章:

java - JSF h :outputStylesheet converter - dynamic css?

java - 通过 JAVA_OPTS 将包含空格的系统属性传递给 Tomcat

java - spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults 有什么用?

java - Long 的大小为 8 字节,那么在 JAVA 中如何将 'promoted' 转换为 float (4 字节)?

java - 当 Handler 启动的 Runnable 抛出异常时,包括来自原始线程的方法调用

java - Android LambdaJ - 无法使用,每次调用都会出现异常

java - 了解 Java 中的检查异常与非检查异常

python - 在 except 中引发异常而不调用原始异常

java.lang.RuntimeException : Resource not found?

java - RuntimeException - 由 : android. database.sqlite.SQLiteException 引起:没有这样的表:tbl(代码 1)