异常的出理

异常的处理有二个方式:try...catch  throws

public class MyException extends Exception{//定义一个异常类,该类继承父类Exception

  String Message;//声明一个成员变量

  public void MyException(String errExpetion){//构造方法,初始化该类的变量

    Messsage=errException;

  }

  public String getMessage(){//覆盖父类Exception中的这个方法,重写

    rentrun Message;

  }

}

           public class Me{ 

             public 返回类型 方法名(参数)throws MyException{

               if(判断参数是否异常){

                 System.out.println("自定义异常的内容");  

               }else{

                 return 方法内容;//返回给返回值

               } 

             }

             public static void main(String[] args){

               Me me=new Me();

               try{

                 对象调用方法;

               }

               catch(MyException e){

                 System.out.println(e.getmessage);//自定义异常  

               }

               catch(Exception e){

                 System.out.println("其它异常");

               } 

             }

           }

 

自定义异常:

public class MyException extends Exception{

  public MyException(String errException){

    super(errException);

  }

}

posted @ 2016-07-18 20:11  对与错  阅读(87)  评论(0编辑  收藏  举报