二十三、Java基础之异常及异常处理机制

一、异常定义
/*
1.异常是什么?
第一,异常模拟的是现实世界不正常的现象
第二,java中采用类的形式去模拟异常
第三,类是可以模拟对象的
NullPointerException e =0x1234
e是引用类型,e中保存的内存地址指向堆中的对象
这个对象一定是NullPointerException类型
这个对象就表示真实存在的异常事件
NullPointerException是一类异常
2.异常处理机制作用?
java语言为我们提供一种完善的处理机制
作用是:程序发生异常事件之后,为我们输出详细的信息
程序员跳过通过这么信息,可以对程序进行一定的处理,使程序更加健壮
*/
public class ExceptionTest01 {

    public static void main(String[] args){

        int a=10;
        int b=0;

        if(b!=0){
            int c=a/b;
            System.out.println(c);
        }else{
            System.out.println("除数不能为0");

        }

    }
}

二、异常处理方式

/*
处理异常有两种方式:
1.声明异常抛出 throws
2.捕捉异常:try....catch....
*/
第一种:
import java.io.*;

public class ExceptionTest02{

    //public static void main (String[] args) throws FileNotFoundException {
    //public static void main (String[] args) throws IOException {
    public static void main (String[] args) throws Exception {


        //创建文件输入流,读取文件
        //未报告的异常错误java.io.FileNotFoundException; 必须对其进行捕获或声明以便抛出
        FileInputStream file=new FileInputStream("E:\666.txt");

    }
}
/*
深入throws
*/
import java.io.*;


public class DeepThrows {

    public static void main(String[] args)throws IOException{

        try{
            m1();
        }catch (FileNotFoundException e){
            System.out.println(e);
        }finally {
            System.out.println("1");
        }


    }
    public static void m1()throws IOException{
        m2();
    }
    public static void m2()throws IOException{
        m3();
    }
    public static void m3() throws IOException{
        new FileInputStream("E:\666.txt");

    }

}

第二种

/*
处理异常的第二种方式:
捕捉:try...catch
语法:
try{
可能过出现异常的代码;
}catch(异常类型1 变量1){
处理异常代码;
}catch(异常类型2 变量2){
处理异常代码;
}

1.catch语句块可以写多个
2.但是从上到下catch,必须从小类型到大类型异常进行捕捉
3.try.....catch中最多执行一个catch语句块,执行完一个try...catch就结束了
*/

--以下代码编译无法通过,因为FileNotFoundException没有处理
import java.io.*;
public class TryCatch {

    public static void main(String[] args){
        //以下代码编译无法通过,因为FileNotFoundException没有处理
      
        try{
            FileInputStream file=new FileInputStream("E:\666.txt");

        }catch(ArithmeticException e){

        }
        
}
}

--

public class TryCatch {

    public static void main(String[] args){
 //编译通过
 
        try{
            FileInputStream file=new FileInputStream("E:\666.txt");

        }catch(FileNotFoundException e){

        }
  }
} 

--

public class TryCatch {

    public static void main(String[] args){

//以下程序无法通过
       
        try{
            FileInputStream file=new FileInputStream("E:\666.txt");
            file.read();

        }catch(FileNotFoundException e){

        }
  }
}     

--//以上程序这样写

import java.io.*;
public class TryCatch {

    public static void main(String[] args){
        try{
            FileInputStream file=new FileInputStream("E:\666.txt");
            file.read();

        }catch(FileNotFoundException e){

        }catch(IOException E){}
 }
} 

--

//编译通过,但是异常太广泛
        /*
        try{
            FileInputStream file=new FileInputStream("E:\666.txt");
            file.read();

        }catch(IOException e){

        }
import java.io.*;
public class TryCatch {

    public static void main(String[] args){
//编译不能通过
        // 已捕获到异常错误java.io.FileNotFoundException
        //重点:catch可以写多个,但是必须是从上到下,从小到大捕捉
        
        try{
            FileInputStream file=new FileInputStream("E:\666.txt");
            file.read();

        }catch(IOException e){

        }catch(FileNotFoundException e){
            //已捕获到异常错误java.io.FileNotFoundException
        }
        
    }
}

三、异常的几种情况

第一种

public class Test03 {

    //编译不通过

    public static void main(String[] args)throws FileNotFoundException {

        FileInputStream file=new FileInputStream("E:\666.txt");
        file.read();//IOExceptionz异常大于FileNotFoundException 
} }

第二种

public class Test03 {

 //编译通过
//IOException包含了FileNotFoundException 
public static void main(String[] args)throws IOException {
  FileInputStream file
=new FileInputStream("E:\666.txt");
   file.read();
}

}

第三种

public class Test03 {
     public static void main(String[] args) {
        try{
        FileInputStream file=new FileInputStream("E:\1666.txt");
        //上面出现异常,try语句块不再执行,进入catch语句块
        System.out.println("Hello World!");
        file.read();
        }catch (FileNotFoundException e){
            System.out.println(e);
            System.out.println("读取文件不存在!");

        }catch(IOException E){
            System.out.println("其他IO异常!");
        }

        System.out.println("ABC");
    }

}

四、getMessage与printStracakTrace

import java.io.*;

public class Test04 {
    public static void main(String[] args){

        try{
            FileInputStream file=new FileInputStream("E:\1666.txt");
            //上面出现异常,try语句块不再执行,进入catch语句块
            System.out.println("Hello World!");

        }catch (FileNotFoundException e){
            //打印异常堆栈的信息
            //一般情况下都会使用该方法去调试程序
            e.printStackTrace();

            /*
            ABC
            java.io.FileNotFoundException: E:1666.txt (系统找不到指定的文件。)
                at java.io.FileInputStream.open0(Native Method)
                at java.io.FileInputStream.open(FileInputStream.java:195)
                at java.io.FileInputStream.<init>(FileInputStream.java:138)
                at java.io.FileInputStream.<init>(FileInputStream.java:93)
                at com.study.异常.Test04.main(Test04.java:14)
             */
            String msg=e.getMessage();
            System.out.println(msg);//E:1666.txt (系统找不到指定的文件。)
        }
        System.out.println("ABC");

    }
}
/*
五、关于finally语句块:
1.finally语句块可以直接和try语句块联用,try....fianlly...
2.在finally语句块中的代码是一定会执行的
3.try.....catch.....finally
*/
第一种:
//finally中的内容会执行
import java.io.FileInputStream;

public class TestFinally {

    public static void main(String[] args){

        try{
            System.out.println("ABC");
            return;
        }finally{
            System.out.println("我还是执行了!");//finally中的内容会执行
        }

    }
}

第二种:

//抛出异常了,finally语句块依然执行
import java.io.FileInputStream;

public class TestFinally {

    public static void main(String[] args){

       try{
            FileInputStream file=new FileInputStream("E:/666.text");
            System.out.println("AAA");
        }finally{
            System.out.println("BBB");//抛出异常了,finally语句块依然执行
        }

    }
}

第三种:

//只要在执行finally语句块之前退出了JVM,则finally语句块不会执行
import java.io.FileInputStream;

public class TestFinally {

    public static void main(String[] args){

//只要在执行finally语句块之前退出了JVM,则finally语句块不会执行
        try{
            System.exit(0);//退出jvm
        }finally{
            System.out.println("AAA");//不会执行
        }
    }
}

六、深入finally

/*
finally语句块中是一定会执行的,所以通常在程序中为了保证某资源的释放,
所以一般在finally语句块中释放资源
*/
import java.io.FileInputStream;

public class TestFinally {

    public static void main(String[] args){
        int m=m1();
        System.out.println(m);//10
    }
        //深入fianlly语句块
    public static int m1(){
        int i=10;
        try{
            return i;//10
        }finally{
            i++;
            System.out.println("finally中的i="+i);//11
        }
    }



}    
/*
finally语句块中是一定会执行的,所以通常在程序中为了保证某资源的释放,
所以一般在finally语句块中释放资源
*/
import java.io.*;

public class TestFinallyDeep {

    public static void main(String[] args){
        FileInputStream file=null;
        try{
            file=new FileInputStream("E:/666.txt");
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }finally{
            //为了保证资源一定会释放
            if (file!=null){
                try{
                    file.close();
                    System.out.println("AAA");
                }catch(IOException e){
                    e.printStackTrace();
                }
            }


        }
    }
}
原文地址:https://www.cnblogs.com/chushujin/p/10127842.html