自定义异常 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("无药可救!");
}
}
}

猜你喜欢

转载自blog.csdn.net/xiexaioyao/article/details/78906256