捕获异常
收集异常
打印异常
public static void main(String[] args) {
String a;
String b;
try {
if("a"=="a")
throw new Exception("操作失不能为空!");
} catch (Exception e) {
TODO: handle exception
e.getMessage();
b=e.getMessage();
}
System.out.println(b);
}
运行main方法
一般丢出异常就是
前面写判断语句 抛出异常 程序就不会再继续往下进行
throw new Exception(“操作失不能为空!”);
这里加了try 和catch
为了捕获到异常的信息 定义一个string类型的参数
去接受错的信息 最后再把错误信息打印出来
这样程序依然往下进去 错误信息存在与自己定义的 b中