Erlo

异常。

2019-04-25 18:01:51 发布   145 浏览  
页面报错/反馈
收藏 点赞

Throwable 类:

javal.lang.Throwable :是Java中所有错误或异常的父类。

子类:1、Exception: 编译期异常:子类:RuntimeException:运行期异常。处理后程序可继续运行

   2、Error:错误, 必须修改后才能继续运行。

异常产生过程的解析:

 public static void main(String[] args) {
    int[] arr={1,2,3};
    int result= getElement(arr,3);
        System.out.println(result);
    }
    public static int getElement(int[] arr,int index){
        return arr[index];
    }

1:数组访问越界,JVM出现异常,做两件事:

  第一:JVM根据异常产生的原因,创建一个异常对象,这个对象包含异常产生  

     内容,原因,位置。new ArrayIndexOutOfBoundsException("3")

  第二:在getElement方法中,没有逻辑处理(try..catch..),JVM会将对象给调用该

     方法的main方法处理。

2:main方法接收到了异常对象,但main也没有处理程序,所以给main的调用者,JVM。

3:JVM做两件事:第一:打印异常对象。第二:JVM会终止程序。

处理异常:

1、throw关键字:

  可以使用throw关键字在指定的方法中抛出指定的异常。

2、使用格式:

  throw new xxxException(“异常产生的原因”)

3、注意:

  1、throw关键字必须写在方法的内部。

  2、throw关键字后面new的对象必须是Exception或其子类对象。

  3、throw抛出异常,我们就必须处理:

      如果是RuntimeException 或其子类的对象,默认给JVM处理(打印,中断)

      如果是编译异常,要么throws,要么try catch。

 public static int getElement(int[] arr,int index){
        if (index<0||index>arr.length-1){
            throw new IndexOutOfBoundsException("数组访问越界");
        }
        return arr[index];
    }

异常处理第一种方式:声明异常throws:

 交给别人处理:

作用:

  当方法内部抛出异常对象的时候,那么我们就必须处理这个异常对象。

  可以使用throws关键字,把异常对象给方法的调用者处理,最终是给JVM处理。

使用格式:在方法声明时使用。

  修饰符 返回值类型 方法名()throws AAAException,BBBException{

  throw new AAAException();

  throw new BBBException();

  }

注意:

  1、throw 关键字必须写在方法声明处。

  2、异常必须是Exception或其子类。

  3、如果方法内有多个异常, 也要声明多个异常。如果有父子类关系,申明父类即可。

  4、调用了一个申明了异常的方法,就必须处理异常:要么继续抛出,给JVM。要么自己处理。

public static void main(String[] args) throws FileNotFoundException {
        readFile("d\a.txt");
    }
    public  static void readFile(String fileName) throws FileNotFoundException{
        if (!fileName.equals("C:\a.txt")){
            throw new FileNotFoundException("文件名错误");
        }
    }

缺陷:JVM处理异常后,会中断程序,后续代码不会执行。

异常处理第二种方式:try...catch:

自己处理。

格式:

  try{

    可能产生异常的代码

  }catch(定义一个异常变量,用来接收try中抛出的异常对象){

  异常的逻辑处理。一般在工作中会把异常信息记录到日志中。

  }catch(){

  }   catch可有多个。

注意事项:

  1、try中可能有多个异常对象,可以用多个catch处理。

  2、try中产生了异常,则执行catch中的逻辑处理。

 public static void main(String[] args)  {
        try {
            readFile("d\a.txt");
        } catch (FileNotFoundException e) {
            System.out.println("文件名输入错误!");
        }
        System.out.println("后续代码");
    }
    public  static void readFile(String fileName) throws FileNotFoundException{
        if (!fileName.equals("C:\a.txt")){
            throw new FileNotFoundException("文件名错误");
        }
    }

Throwable处理异常的三个方法:

1、String getMessage()  返回异常的简短描述。

2、String toString() 异常的详细消息字符串。

3、Void printStackTrace()  最全面的错误信息。

 public static void main(String[] args)  {
        try {
            readFile("d\a.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
            System.out.println(e.toString());
            e.printStackTrace();
        }
        System.out.println("后续代码");

finally:

无论是否出现异常,都会执行。

一般用于,资源释放。

多异常处理:

一个try多个catch:

  注意:catch里定义的异常变量,如果有子父类关系,要把子类异常变量写在上边。

     因为:抛出的异常对象,会从上到下给catch,如果父类写在上面,因为多态,                            后面的catch不会被用到,会出错。

多个异常,一次捕获,多次处理,多态,变量用Exception类型。

子父类异常:

父类异常时怎么样,子类异常就怎么样:

  子类重写父类方法时,可以:

    1、抛出和父类相同的异常。2、抛出父类异常的子类异常。3、不抛出异常

    4、父类方法没有抛出异常时,子类只能捕获处理,不能抛出。

自定义异常:

格式:

  public class XXXException extends Exception | RuntimeException{

    空参构造方法

    异常信息构造方法

    }

注意:

  1、一般以Exception结尾。

  2、必须继承Exception 或  RuntimeException

    :Exception:编译期异常,抛出或捕获处理

    :RutimeException:运行时异常,JVM处理,中断。

public class MyException extends Exception {
    public MyException(){
        super();
    }
    public MyException(String message){
        super(message);
    }
}

自定义异常练习:

 

public class main {
    static String[] username={"Sam","Penny"};
    public static void main(String[] args)  {
        System.out.println("输入要注册的用户名:");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.next();
        CheckName(name);
    }
    public  static  void CheckName(String name){
        for (String username:username){
            if (username.equals(name)){
                try {
                    throw new MyException("用户名已存在");
                } catch (MyException e) {
                    System.out.println(e);
                    return;
                }
            }
        }
        System.out.println("注册成功");
    }

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认