違例差錯控制需要注意的地方

來源:互聯網
上載者:User

重新拋出違例
                     //throw e.fillInStackTrace();                     //throw new NullPointerException();
使用這兩種方式重新拋出違例將會丟失違例的起源資訊
RunntimeException代表的是一個 編程錯誤,不需要我們去刻意的控制,程式員應該盡量的避免這樣的錯誤,這樣有助於我們進行調試
丟失的違例
public  class MainClass
{
        /**        *  @param args        */         public  static  void main(String[]
args)  throws Exception{
              //異常的丟失             LostException le =  new LostException();               try {                    le.i();             }  finally{                    le.n();             }                    }
}
class LostException{         public  void n()  throws Exception
{               throw  new NormalException();       }         public  void i()  throws Exception
{               throw  new ImportantException();       }}
class  NormalException  extends Exception{        @Override         public String
toString() {              //  TODO Auto-generated
method stub               return "a
normal Exception" ;       }}
class  ImportantException  extends Exception{        @Override         public String
toString() {              //  TODO Auto-generated
method stub               return "a
important Exception" ;       }}
ImportantException將會捕獲不到,會被覆蓋
違例限制
當有繼承關係時,子類違例變窄了
finally清除
當在構建器拋出違例時,一般不要嘗試finally清除
違例匹配準則
從窄到寬,一旦匹配到則不會再進行向下尋找控制器進行匹配
catch(FileNotFoundException e){}catch(Exception e){}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.