0
点赞
收藏
分享

微信扫一扫

Java基础之异常


异常

Throwable类

Java中所有异常的超类,在Java中所有的异常,错误的基类就是Throwable类

Throwable

  • Exception 异常
  • Error 错误

Throwable常用方法

Constructor:

  • Throwable()
    Throwable构造方法,Throwable类对象中,存储的异常或者错误信息默认为null
  • Throwable(String message)
    Throwable构造方法,Throwable类对象中,存储的异常或者错误信息为message

Method:

  • String getMessage()
    获取Throwable对象中存储的异常或者错误信息
  • String toString()
    返回当前异常或者错误的简要描述
  • void printStackTrace()
    展示错误的前因后果,[字体是红色的]

package cn.ocean888_a;

public class Demo1 {
public static void main(String[] args) {
Throwable throwable = new Throwable("test");

System.out.println(throwable.getMessage());
System.out.println(throwable.toString());
// throwable.printStackTrace();
}
}

Java基础之异常_抛出异常

throwable.printStackTrace

Java基础之异常_抛出异常_02

package cn.ocean888_a;

public class Demo1 {
public static void main(String[] args) {
Throwable throwable = new Throwable("test");

test();
}

public static void test() {
new Throwable("throwableTest").printStackTrace();
}
}

Java基础之异常_空指针异常_03

Exception和Error

Exception 异常可以处置

Error 错误,不可处置,只能避免

异常处理

捕获异常

try catch 结构

try catch finally结构

package cn.ocean888_a;

public class Demo1 {
public static void main(String[] args) {
int[] arr = new int[10];
// 算数异常
div(10, 0, arr);
// 算数异常
div(10, 0, null);
// 空指针异常
div(10, 2, null);
}

public static void div(int num1, int num2, int[] arr) {
try {
arr[0] = num1 /num2;
} catch (ArithmeticException e) {
System.out.println("算数异常");
} catch (NullPointerException e) {
System.out.println("空指针异常");
}
}
}

Java基础之异常_构造方法_04

总结:

  1. 代码中从异常发生的位置开始,之后的代码都不再执行
  2. 代码中有多个异常,可以使用多个catch块进行捕获操作,分类进行处理
  3. 后期可以将异常情况保存下来作为log日志文件
  4. 异常被捕获之后代码可以正常运行

抛出异常

  • throw
    方法内抛出异常
  • throws
    在方法声明位置,告知调用者当前方法有哪些异常抛出
    异常声明需要生成对应的文档注释

package cn.ocean888_a;

public class Demo1 {
public static void main(String[] args) throws ArithmeticException, NullPointerException {
int[] arr = new int[10];
try {
div(10, 0, null);
} catch (ArithmeticException e) {
System.out.println(e);
} catch (NullPointerException e) {
System.out.println(e);
}
}

/**
* 测试除法函数
* @param num1 被除数
* @param num2 除数
* @param arr 保存数组
* @throws ArithmeticException 算数异常
* @throws NullPointerException 空指针异常
*/
public static void div(int num1, int num2, int[] arr) throws ArithmeticException, NullPointerException {

if(num2 == 0) {
// 除数为零算数异常
throw new ArithmeticException("算数异常");
}

if(arr == null) {
// 数组空指针异常
throw new NullPointerException("空指针异常");
}

System.out.println(num1 / num2);
arr[0] = num1 / num2;
}
}

Java基础之异常_构造方法_05

抛出异常总结:

  1. 一个代码块{}内,有且只能有一个异常
  2. 从throw位置开始,之后的代码不在运行
  3. 代码中存在使用throw抛出异常,在方法声明位置必须告诉调用者这里有什么异常

RuntimeException

运行时异常

Java基础之异常_空指针异常_06

自定义异常

自定义异常类名:必须以Exception结尾

package cn.ocean888_a;

public class Demo2 {
public static void main(String[] args) {
// 抛出异常
try {
buyOneFreeOne(false);
} catch (Exception e) {
System.out.println(e);
}
}

public static void buyOneFreeOne(boolean single) throws NoGirlFriendException{
if(single) {
throw new NoGirlFriendException();
}
System.out.println("买一送一");
}
}


// 自定义异常
class NoGirlFriendException extends Exception {
// 无参数构造方法
public NoGirlFriendException() {}

// 有参数构造方法
public NoGirlFriendException(String message) {
super(message);
}
}

Java基础之异常_构造方法_07


举报

相关推荐

0 条评论