天天看點

自定義異常

在某些應用中,程式設計人員可以根據程式的需要建立自己的異常類和異常對象,用來處理程式中特定的邏輯運作錯誤或異常

自定義異常文法

class NewException extends OldException
{
......
}      

自定義異常舉例

三角形邊長為負值異常
class TriangleMinusLengthException extends Exception
{
    String name;
    public TriangleMinusLengthException(String str)
    { 
    name = str;
    }
    public String toString()
    {
  return name;
  }
}      
不滿足三角不等式異常
class TriangleUnequationException extends Exception
{
    String name;
    public TriangleUnequationException (String str)
    {
    name = str;
    }
    public String toString()
    {
  return name;
  }
}      

繼續閱讀