自定義異常 RuntimeException //Exception這兩個的區別用法



自定義異常  RuntimeException //Exception這兩個的區別用法


Exception類中有一個特殊的子類異常RuntimeException 運行時異常

如果在函數內容拋出該異常 函數上不用聲明,編譯一樣通過

如果在該函數上聲明瞭該異常,調用者可以不用運行處理,編譯一樣通過


   如果聲明是 Exception  必須進行捕抓  編譯時異常

class  Demo
{
 public static void main(String[] args)
 {
  Exceptionc f=new Exceptionc();
     
  f.Cold(-2);
  
 }
}
 
class people{

private String name ;

private int    Age;

public  people(){
  
   this.name=name;

   this.Age=Age;
   }

public void Study (){

System.out.println("正常學習功能");

}

public void ge(String name,int Age){

  System.out.println("姓名:"+name+"年齡"+Age);

}
}  

class abnormal extends RuntimeException //Exception
{
public abnormal(String mag){
 super(mag);
 }
 
    
class  Exceptionc
{
 

 public  void Cold (int  a)  throws   abnormal
{
 if(a==1)
 {
 people f = new people();
 f.ge("李四",18);
 f.Study();
 }
   else if(a==0)
  throw new abnormal(" 輕度生病");
   else if(a<1)
  throw new abnormal("無藥可救!");
}
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章