qiyadeng

          專注于Java示例及教程
          posts - 84, comments - 152, trackbacks - 0, articles - 34

          Swing中異常處理

          Posted on 2008-07-29 11:18 qiyadeng 閱讀(1484) 評論(0)  編輯  收藏

           

          每個Java程序員都要處理異常,異常處理在應用程序中起著重要的作用。在Java世界中,處理異常看上去不是那么簡單,不僅僅是在異常發生的地方,優雅的報告異常。

          使用ThreadGroup處理異常

          ThreadGroup類中在這個時候大部分方法已經作廢了,但是該類還是很有用的。ThreadGroup類有一個方法uncaughtException(Thread,Throwable),意思是:當線程組中的線程因為異常而終止時,Java虛擬機調用該方法。這樣你可以考慮繼承ThreadGroup類來處理你應用中沒有捕獲的異常。

          public class AppSpecificThreadGroup extends ThreadGroup {

          public void uncaughtException(Thread, Throwable) {

          // app specific error handling here

          // ex: if fatal, release resources

          // show user dialog

          }

          }

          這樣在你的應用啟動的時候,把你的線程加入到線程組(Thread group)中。

          public class ApplicationMain {
            public static void main(String[] args) {
              Runnable r = new ApplicationStarter(args);
              ThreadGroup g = new AppSpecificThreadGroup();
              Thread t = new Thread(g, r);
              t.start();
            }
            
            private static void doStart(String[] args) {
              // start application here...
            }
            
            private static class ApplicationStarter {
              private String[] args;
              ApplicationStarter(String[] args) {
                this.args = args;
              }
              
              public void run() {
                doStart(args);
              }
            }
          }

          使用Java5的Thread.UncaughtExceptionHandler

          當認識到ThreadGroup.uncaughtException(Thread,Throwable)方法的巨大作用時,Sun團隊在Java5版本站中增強了未捕獲的異常處理的功能,引入了Thread.UncaughtExceptionHandler接口,另外這個接口引入了兩個新的”Check points”用于處理異常。Thread類有兩個屬性-uncaughtExceptionHandler和defaultUncaughtExceptionHandler,它們有相應的get/set方法。但是這兩個方法是不同的。讓我們看看處理異常的過程。

          n 使用uncaughtExceptionHandler為當前線程處理異常。

          n 如果uncaughtExceptionHandler為空(null),使用線程組(如果用戶沒有顯示設置,線程組是uncaughtExceptionHandler的默認實現)。

          n 如果線程組是默認的實現,但是不是根線程組,將把錯誤處理委派給父線程組。

          n 如果線程組是默認的實現,并且是根線程組,將使用線程(Thread)類上設置的defaultUncaughtExceptionHandler方法。

          n 如果在線程類沒有設置defaultUncaughtExceptionHandler,將調用Java5之前的ThreadGroup類中的未捕獲異常方法。

          實現Thread.UncaughtExceptionHandler接口看起來也像上面實現ThreadGroup接口。

          public class AppSpecificExceptionHandler implements Thread.UncaughtExceptionHandler {
            
           public void uncaughtException(Thread, Throwable) {
             // app specific error handling here
             // ex: if fatal, release resources
             //     show user dialog
           }
          }

          在應用程序中插入如下:

          public class ApplicationMain {
           public static void main(String[] args) {
            Thread.setDefaultUncaughtExceptionHandler(new AppSpecificExceptionHandler());
           }
          }

          Swing異常處理實現

          許多人沒有注意到現有的JOptionPanel類,該類中有許多的優美的彈出對話框的靜態方法(showOptionDialog,showConfirmDialog,showInputDialog,和showMessageDialog),這些方法接受的參數不僅僅是字符串。這些靜態方法(也包括JOptionPanel構造函數)都是接受一個Object作為參數的的。下面是一個實現該類的核心代碼:

          public class ErrorDialog {

          public ErrorDialog() {

          super();

          }

          public static void showQuickErrorDialog(JFrame parent, Exception e) {

          final JTextArea textArea = new JTextArea();

          textArea.setFont(new Font("Sans-Serif", Font.PLAIN, 10));

          textArea.setEditable(false);

          StringWriter writer = new StringWriter();

          e.printStackTrace(new PrintWriter(writer));

          textArea.setText(writer.toString());

          JScrollPane scrollPane = new JScrollPane(textArea);

          scrollPane.setPreferredSize(new Dimension(350, 150));

          JOptionPane.showMessageDialog(parent, scrollPane,

          "An Error Has Occurred", JOptionPane.ERROR_MESSAGE);

          }

          }

          在程序中的一個例圖如下:

          clip_image002


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 五大连池市| 甘洛县| 南汇区| 通海县| 镇江市| 盐池县| 珲春市| 清镇市| 肃南| 富锦市| 顺义区| 宣化县| 淮北市| 宁德市| 合肥市| 蕲春县| 苍南县| 闽侯县| 卢龙县| 特克斯县| 阿拉善左旗| 邢台县| 旬阳县| 徐闻县| 依兰县| 岳普湖县| 灵山县| 漳州市| 芒康县| 肥城市| 华池县| 三明市| 霸州市| 唐河县| 霍林郭勒市| 九龙县| 齐齐哈尔市| 广州市| 延长县| 平凉市| 和田县|