[英]How to catch all checked exceptions (in a single block) in Java 6?
重要提示:此问题仅与 Java 6(及更低版本)有关。
这里的层次结构显示 Java Exception
分为两种类型: RuntimeException
和[not a RuntimeException] :
把它分成像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
// ...
}
但这似乎非常骇人听闻。 有没有更好的办法?
如果我理解正确,那么您就快到了。 只需捕获运行时异常。 这将捕获 RuntimeException 及其层次结构中的所有内容。 然后是 Exception 的失败,你会被覆盖:
try { transaction.commit(); } catch (RuntimeException e) { // Throw unchecked exception throw e; } catch (Exception e) { // Handle checked exception // ... }
Java 7
允许你这样的结构:
try {
transaction.commit();
} catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException e ) {
// blablabla
}
UPD:我认为,在早期版本的 Java 中没有很好和方便的方法来做到这一点。 这就是Java
语言开发人员在Java 7
引入这种结构的原因。 因此,您可以为Java 6
设计自己的方法。
我在另一篇文章中给出了类似的答案,所以是的,它是复制过去的:
我所做的就是拥有一个处理所有异常的静态方法,并将日志添加到 JOptionPane 以将其显示给用户,但您可以将结果写入FileWriter
中的文件中,该文件包装在BufeeredWriter
。 对于主要的静态方法,要捕获未捕获的异常,我会这样做:
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...
}
}
我希望我有所帮助。
祝你今天过得愉快。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.