我不確定如何管理GUI中的異常;我的目標是讓用戶知道是否出現錯誤並顯示可理解的消息。在Swing GUI中處理異常
我在考慮做這樣的事情:
// I'm inside an actionPerformed() method
try {
// do whatever I have to do here
} catch (KnownBusinessException1 e) {
// inform the user and do something;
// most times simply inform the user that it wasn't possible to complete the
// operation and remain in the same window instead of moving forward.
} catch (KnownBusinessException2 e) {
// just as above
} catch (KnownDataAccessException1 e) {
// just as above
} catch (KnownDataAccessException2 e) {
// just as above
} catch (RuntimeException e) { // I want to catch any other unexpected exception,
// maybe NPE or unchecked IllegalArgumentExceptions and so on
// something went wrong, I don't know where nor how but I will surely inform the user
}
現在:如果try塊有檢查的異常趕上,那會是更好的嵌套的try/catch或捕獲這些檢查捕獲RuntimeException之後的異常? (它可能取決於,我甚至不知道這是否會發生btw)
另一件事:Error
是什麼?如果我是一個用戶,我不希望遇到意外的關機,我寧願讓應用告訴我有什麼地方出現了令人難以置信的錯誤,並且沒有人能對此做任何事情,「世界的盡頭即將到來,所以我將立即退出「。至少我會知道那不是我的錯。
順便說一句不知道這是否是一個很好的做法,以發現錯誤...:\
有一種更好的方式在Swing應用程序做到這一點?
看看本作的錯誤http://stackoverflow.com/questions/352780/when-to-catch-java-lang-error –
[我怎樣捕獲事件調度線程(EDT)異常?](http://stackoverflow.com/questions/4448523/how-can -i-catch-event-dispatch-thread-edt-exceptions) – trashgod
@BheshGurung我已經讀過它了。那麼,如果沒有問題,我想我會在RuntimeException catch塊之後添加另一個catch塊,以便捕獲錯誤並在退出之前嘗試通知用戶。 @ trashgod:對我來說這似乎不是重複:) – tmh