异常

Throwable 类

Throwable t = new Throwable();
System.out.println(t.toString());
//全限定名 全类名
——>java. lang. Throwable
Throwable t = new Throwable();
System.out.println(t.getMessage());
——> null
//getMessage()
Throwable t = new Throwable("懒癌");
System.out.println(t.getMessage());
——> 懒癌
//printStackTrace()
public class Test{
  public static void main(String[] args) {
    test();
  }
public static void test(){
  Throwable t = new Throwable ("懒癌");
  t.printStackTrace();
  }
}
java.lang.Throwable: cancer
    at com.TurtleGraph.Test.test(Test.java:16)
    at com.TurtleGraph.Test.main(Test.java:13)
image.png

已知直接子类:

  • Error: Error 是 Throwable 的子类,用于指示合理的应用程序不应该试图捕获的严重问题。大多数这样的错误都是异常条件。虽然 ThreadDeath 错误是一个“正规”的条件,但它也是 Error 的子类,因为大多数应用程序都不应该试图捕获它。
    Java虚拟机无法解决的严重问题。
    Error是jvm或硬件出现的错误,一般不用编写代码取处理
    如:JVM系统内部错误、资源耗尽等严重情况。一般不编写针对性的代码进行处理。
  • Exception: 其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。
    Exception 类及其子类是 Throwable 的一种形式,它指出了合理的应用程序想要捕获的条件。
    子类:
    运行时异常,不用显式处理
    检查性异常,显式处理,不是语法错误
//检查性异常
public static void main(String[] args){
  try{
      Class.froName("");
  }catch(Exception e){

  }
}

Exception是需要代码去进行处理
例如:
1 空指针访问
2 试图读取不存在的文件
3 网络连接中断

public static void main(String[] args){
    test(2,0);  
  }
public static void test(int a, int b){
    //当系统直行道a/b时,发现b是0; 由于数学上要求出书不能为0,生成异常对象,调用异常对象的printStackTrace方法
    int c = a/b;
    System.out.println(c);
  }
——> ArithmeticException 数学异常

捕获异常:

public static void main(String[] args){
    test(2,0);  
  }
public static void test(int a, int b){
    //当系统直行道a/b时,发现b是0; 由于数学上要求出书不能为0,生成异常对象
    //把可能出现异常的代码放进try里
    try{
      int c = a/b;
      
    }catch(Exception e){
      System.out.println("除数不能为0");
    }
  System.out.println("hello world");
  }
——> 除数不能为0
    hello world

捕获异常需要注意的细节:
1.try块中的异常经过处理,try块后的代码可以正常运行
2.如果try块里出现了异常代码,try块里异常代码之后的代码就不会执行
3.一个try块后面是可以跟多个catch块,也就是一个try块可以捕获多种类型异常
4.一个try块可以捕获多种类型异常,但是异常的类型必须从小到大进行捕获,否则编译报错

抛出处理

抛出处理要注意的细节

    public static void main(String[] args) {
        

    }
    public static void test(){
        //throw throws
        try {
            throw new Exception();
        }catch (Exception e){
            
        }
    }
}
    public static void main(String[] args) {
        try{
            test();
        }catch (Exception e){
            
        }

    }

    public static void test() throws Exception {
        //throw throws
        throw new Exception();
    }
}
    public static void main(String[] args) throws Exception{
            test();

    }

    public static void test() throws Exception {
        //throw throws
        throw new Exception();
    }
}

1.如果一个方法内部抛出了异常,那么必须要在方法上声明抛出
2.如果调用一个声明抛出异常的方法,那调用者必须处理
3.如果一个方法内部抛出一个异常对象,那么throw语句后面的代码都不会执行,该方法马上停止
4.在一种情况下,一次只能抛出一种异常对象
谁调用谁处理,如果没有处理就是检查性异常

throw & throws

1.throw关键字是用于方法内部,throws用于方法声明
2.throw用于方法内部抛出一个异常对象,throws用于方法声明抛出异常类型
3.throw后只能跟一个异常对象,throws一次可以跟多个异常类型

自定义异常

步骤:
自定义一个类继承Exception

class NoIpException extends Exception{

}
//模拟FQ,如果没插网线,请插上网线
public class Test {
    public static void main(String[] args) {
        try{
            login(null);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("请插网线");
        }

    }
    public static void login(String ip) throws NoIpException{
        if (ip == null){
            throw  new NoIpException();
        }else {
            System.out.println("您的好友列表: ");
        }
    }
    

}
class NoIpException extends Exception{

}
import org.omg.PortableServer.ThreadPolicyOperations;

import java.util.Scanner;

/**
 * Created by Administrator on 2018/5/30.
 */
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int money = scanner.nextInt();
               try {
                   payment(money);
               }catch (Exception e){
                   e.printStackTrace();
                   System.out.println("霸王餐");
               }
    }
    public static void payment(int money) throws NoEnoughMoney{
        if (money < 20){
            throw new NoEnoughMoney();
        }else {
            System.out.println("paid");
        }
    }
}
class NoEnoughMoney extends Exception{

}

运行时异常&编译时异常

如果方法上声明了一个运行时异常,那么在方法上可以声明也可以不声明,调用者可以处理也可以不处理,如果一个方法内部抛出了一个编译时异常对象,那么方法上就必须要声明,调用者也必须处理
RunTimeException及其子类都是运行异常
RuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类。
可能在执行方法期间抛出但未被捕获的 RuntimeException 的任何子类都无需在 throws 子句中进行声明。

finally块

资源文件使用完一定要解除占用,别的程序无法对该文件进行操作,使用finally的前提一定要使用try块
即使出现异常还是那个也会执行finally块
return, throw 之后finally依旧执行

public class Test {
    public static void main(String[] args) {
        try{
            int c = 1/0;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("执行finally块");
        }
    }
}
——> 执行finally块
public class Test {
    public static void main(String[] args) {
        try{
            int c = 1/1;
            return;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("执行finally块");
        }
    }
}
——> 执行finally块

exit 之后finally不执行
exit:终止当前运行的Java虚拟机

public class Test {
    public static void main(String[] args) {
        try{
            int c = 1/1;
            System.exit(1);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("执行finally块");
        }
    }
}
——> 执行finally块

组合:

try+finally:适合处理运行时异常
try+catch:不用释放资源
try+catch+finally:适合处理异常并且释放资源 Ctrl+alt+t

try{
    ......  //可能产生异常的代码
}
catch( ExceptionName1 e ){
    ......  //当产生ExceptionName1型异常时的处置措施
}
catch( ExceptionName2 e ){
......  //当产生ExceptionName2型异常时的处置措施
}  
[ finally{
......   //无论是否发生异常,都无条件执行的语句
        }  ]
image.png

捕获异常的相关信息

  • getMessage() 获取异常信息,返回字符串。打印出错原因
  • printStackTrace() 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。打印函数调用栈
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容