异常:
Throwable
- Error
- OutOfMemoryError
- Exception
- RuntimeException
- ClassCastException
- NullPointerException
- IndexOutOfBoundsException
- ArithmeticException
- (CheckedException)
- IOException
- SqlException
- RuntimeException
try-catch-finally
- try
- catch
- 可以有多个
- 异常与异常之间的关系
- 没有关系 多个catch块
- 继承关系 子类的catch块放在前面(向上造型)
- 处理方式相同: ClassCastException | IOException
- finally
- try块必须,其他至少二选一
执行代码与我的注解
import java.math.BigDecimal;
public class TryCatchPractice {
public static void main(String[] args) {
char a[] = new char[1];
String str=null;
try {
System.out.println("trycatch练习");
a[7] = 0;
//数组越界
BigDecimal errorbigDecimal = new BigDecimal(3 / 0);
System.out.println(errorbigDecimal);
//算数异常
System.out.println(str.charAt(7));
//空指针异常
//当程序执行到运行时异常时,不管后面还有多少没有执行的语句,都不会继续执行,转为执行catch语句块,但finally必须执行
}
catch (NullPointerException e) {
System.out.println("运行时异常zhi空指针异常");
//空指针一般存在获取的字符串为空时,找不到目标的存在
}
catch (ArithmeticException e) {
System.out.println("运行时异常zhi算数异常");
//算数异常最简单的例子时,除数不能为0,如果为0 ,执行这个异常
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("运行时异常zhi数组越界");
//越界:当获取 的值取出的下标,并不在选择的范围,就执行这个异常
}
catch (Exception e) {
System.out.println("exception异常捕获");
//exception一定的catch一定要最后写,从上往下依次解决,exception异常是这些异常的父类,只能最后写
}
finally {
System.out.println("我一定要运行");
}
System.out.println("这是try—catch语句块外的语句");
}}
执行结果:
trycatch练习
运行时异常zhi数组越界
我一定要运行
这是try—catch语句块外的语句
注意:return 时,finally任然执行;system.exit(0)时,finally 不执行
自定义异常:
Test类
public class Test {
public static void main(String[] args) {
PayMoney payMoney=new PayMoney();
payMoney.payfor(100, "张三");
}
}
支付金额类:PayMoney
public class PayMoney {
public void payfor(double money, String username) {
User user = new User();
user.setPrice(10000);
user.setUsername(username);
if (money>user.getPrice()) {
System.out.println("恭喜您,你花了"+user.getPrice()+"元,买了一个岛");
System.out.println("您的余额为:"+BigDecimal.valueOf(money).subtract(BigDecimal.valueOf(user.getPrice()))+"元");
}else {
try {
throw new CustomRunTimeException("提示: 你输入的金额不够", "去你的,钱都不够,买啥岛 (▼⊿▼)");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("解决办法:买岛,钱不够(^_^)");
}
}
}
}
继承runtimeException
public class CustomRunTimeException extends RuntimeException {
private String currentString;
public String getCurrentString() {
return currentString;
}
public void setCurrentString(String currentString) {
this.currentString = currentString;
}
public CustomRunTimeException() {
super();
// TODO Auto-generated constructor stub
}
public CustomRunTimeException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public CustomRunTimeException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public CustomRunTimeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/*
* 我自定义的异常("抛出异常的内容,抛出异常的原因")
*/
public CustomRunTimeException(String message, String name) {
super(message);
this.setCurrentString(name);
System.out.println("客服提示:"+name);
//保证 值传到异常这个地方
}
public CustomRunTimeException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
执行结果:
注意:抛出runtime异常,可以在编译期不管他,检查不会报错