java——异常类、异常捕获、finally、异常抛出、自定义异常

编译错误:由于编写程序不符合程序的语法规定而导致的语法问题。

运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误。

 java异常类都是由Throwable类派生而来的,派生出来的两个分支分别为ErrorException类。

  Error类:java程序在运行过程中发生的系统内部错误和资源耗尽错误。由java虚拟机生成并抛出,java通常部队这类异常进行处理,而且这类错误很少出现。

  RuntimeException类:程序错误导致的异常。

  非RuntimeException类:(比如,IOException类:本来是可以正常运行的程序,由于某些情况发生异常导致程序不能正常运行。)

package follow_pacage;

public class exp {
    public static void main(String[] args) {
        tryCatch();
    }
    static void tryCatch() {
        try {
            try {
                System.out.println("执行里层try块\n");
                int b = Integer.parseInt("abc");
                System.out.println(b);
            }catch(ArrayIndexOutOfBoundsException e) {
                System.out.println("执行里层catch\n");
            }
        }catch(NumberFormatException e) {
            System.out.println("执行外层catch\n");
        }
    }
}

 异常捕获:

try{
...
}catch(Exception e){
...
}

  我试了一下,try后可以不跟catch,而直接跟fianlly。

finally:

  如果将内存释放的工作放在finally块内,程序不会由于内存未释放资源而导致内存泄漏。

  finally语句必须配合try使用。

  通常情况下,finally语句块总会执行,那么什么时候finally语句块不会执行呢?

    1.cpu关闭

    2.finally之前的语句有Syste.exit(0)

    3.finally过程中抛出异常,则异常之后的而其他程序不会继续执行

    4.程序所属线程死亡。

  如果try或者catch语句块中包含return语句,那么程序在执行return语句之前执行finally语句块。

  不能在finally中执行return、continue语句(https://blog.csdn.net/abinge317/article/details/52253768

异常抛出:

  1)在方法的声明中使用throws语句指定它所抛出的异常

  2)使用throw语句抛出异常

  例子:

package follow_pacage;

public class exp {
    public static void main(String[] args) {
        try {
            String s = null;
            demo(s);
        }catch(NullPointerException e) {
            System.out.println("异常为:"+e);
        }
    }
    static void demo(String s) {
        try {
            if(s == null) {
                throw new NullPointerException("ThrowExample");
            }
        }catch(NullPointerException e){
            System.out.println("参数s的值为空");
            //这里抛出了异常
            throw e;
        }
    }
}

  try catch语句可以处理异常,如果一个方法可以导致一个异常,但并没有用try catch语句来处理异常,就要在方法名后面使用throws声明这个异常,异常将在外层被处理或者继续抛出。

package follow_pacage;

import java.io.IOException;

public class exp {
    public static void main(String[] args) {
        try {
            System.out.println("请输入:");
            String s = getInput();
            System.out.println("输入为:");
            System.out.println(s);
        }catch(IOException e) {
            System.out.println("产生异常:"+e.getMessage());
        }
    }
    public static String getInput() throws IOException{
        char[] buffer = new char[10];
        boolean flag = true;
        int count = 0;
        while(flag) {
            buffer[count] = (char)System.in.read();
            if(buffer[count] != '\n') {
                count++;
            }else {
                flag = false;
            }
            //这里在输入结尾有一个回车,所以实际只存放了9个字符
            if(count >= 10) {
                IOException aException = new IOException("buffer is full");
                //抛出异常
                throw aException;
            }
        }
        //新建了一个String类型的字符串,将char[]类型的buffer字符传入
        return new String(buffer);
    }
}

 自定义异常: 

package follow_pacage;

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

 

posted @ 2018-10-12 10:01  高圈圈  阅读(800)  评论(0编辑  收藏  举报