8

重要提示:此问题仅与 Java 6(及以下)有关。

这里的层次结构显示 JavaException分为两种类型:RuntimeException[不是 RuntimeException]

Java 异常层次结构

UncheckedException将其划分为类似的东西CheckedException不是更好吗?例如,下面的语句有相当多的检查异常:

try {
    transaction.commit();
} catch (SecurityException e) {
} catch (IllegalStateException e) {
} catch (RollbackException e) {
} catch (HeuristicMixedException e) {
} catch (HeuristicRollbackException e) {
} catch (SystemException e) {
}

我只对它是成功还是失败真正感兴趣,因此希望将已检查的异常作为一个组来处理,而不是未检查的异常,因为捕获意外错误并不是一个好习惯。所以考虑到这一点,也许我可以做类似的事情:

try {
    transaction.commit();
} catch (Exception e) {
    if (e instanceof RuntimeException) {
        // Throw unchecked exception
        throw e;
    }
    // Handle checked exception
    // ...
}

但这似乎非常骇人听闻。有没有更好的办法?

4

3 回答 3

9

如果我理解正确,那么你就快到了。只需捕获 RuntimeException。这将在层次结构中捕获 RuntimeException 及其下的所有内容。然后是异常的失败,你被覆盖了:

尝试 {
    事务.commit();
} 捕捉(运行时异常 e){
    // 抛出未经检查的异常
    扔 e;
} 捕捉(异常 e){
    // 处理检查异常
    // ...
}
于 2013-02-08T11:14:22.940 回答
5

Java 7允许你这样的结构:

try {
    transaction.commit();
} catch (SecurityException | IllegalStateException  | RollbackException | HeuristicMixedException  e ) {
   // blablabla
}

UPD:我认为,在早期版本的 Java 中没有很好且方便的方法来执行此操作。这就是Java语言开发人员在Java 7. 因此,您可以为Java 6.

于 2013-02-08T11:13:14.173 回答
0

我在另一篇文章中给出了类似的答案,所以是的,它是复制过去:

我所做的是拥有一个处理所有异常的静态方法,并将日志添加到 JOptionPane 以将其显示给用户,但您可以将结果写入包含FileWriterBufeeredWriter. 对于主要的静态方法,要捕获未捕获的异常,我会这样做:

SwingUtilities.invokeLater( new Runnable() {
    @Override
    public void run() {
        //Initializations...
    }
});


Thread.setDefaultUncaughtExceptionHandler( 
    new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException( Thread t, Throwable ex ) {
            handleExceptions( ex, true );
        }
    }
);

至于方法:

public static void handleExceptions( Throwable ex, boolean shutDown ) {
    JOptionPane.showMessageDialog( null,
        "A CRITICAL ERROR APPENED!\n",
        "SYSTEM FAIL",
        JOptionPane.ERROR_MESSAGE );

    StringBuilder sb = new StringBuilder(ex.toString());
    for (StackTraceElement ste : ex.getStackTrace()) {
        sb.append("\n\tat ").append(ste);
    }


    while( (ex = ex.getCause()) != null ) {
        sb.append("\n");
        for (StackTraceElement ste : ex.getStackTrace()) {
            sb.append("\n\tat ").append(ste);
        }
    }

    String trace = sb.toString();

    JOptionPane.showMessageDialog( null,
        "PLEASE SEND ME THIS ERROR SO THAT I CAN FIX IT. \n\n" + trace,
        "SYSTEM FAIL",
        JOptionPane.ERROR_MESSAGE);

    if( shutDown ) {
        Runtime.getRuntime().exit( 0 );
    }
}

在你的情况下,你可以像我之前告诉你的那样写一个日志,而不是向用户“尖叫”:

String trace = sb.toString();

File file = new File("mylog.txt");
FileWriter myFileWriter = null;
BufferedWriter myBufferedWriter = null;

try {
    //with FileWriter(File file, boolean append) you can writer to 
    //the end of the file
    myFileWriter = new FileWriter( file, true );
    myBufferedWriter = new BufferedWriter( myFileWriter );

    myBufferedWriter.write( trace );
}
catch ( IOException ex1 ) {
    //Do as you want. Do you want to use recursive to handle 
    //this exception? I don't advise that. Trust me...
}
finally {
    try {
        myBufferedWriter.close();
    }
    catch ( IOException ex1 ) {
        //Idem...
    }

    try {
        myFileWriter.close();
    }
    catch ( IOException ex1 ) {
        //Idem...
    }
}

我希望我有所帮助。

祝你今天过得愉快。:)

于 2016-10-27T20:40:21.043 回答