throwable(异常类最顶端的类)
1.空指针异常
Arraylist<Integer> a = null;
list.add(1);
2.角标越界异常
int[] array = new int[5]
System.out.println(arrayu[6]);
3.算数异常
System.out.println(10/0);
程序员处理 异常
try 指测试异常代码
Catch 捕获异常信息
finally 指异常结束后要做的事
try -catch 捕获异常的流程
1.函数中某句代码发生了异常
2.发生异常就产生了对应的异常对象
3.这个异常对象就返回给调用者
情况一 没有对异常进行处理,这时候就会异常交给上级jvm jvm 会使用 默认的处理机制
情况二 调用者使用了 异常处理 就是写了 try catch 这时返回的异常就会去与catch匹配
//测试异常
class TestException{
public int fun(int a ,int b) {
//发生异常一定会产生了一个对应的异常对象
return a / b ;
}
}
TestException test = new TestException();
try {
//可能发生异常的代码
int num = test.fun(10, 0);
System.out.println(num);
} catch(ArithmeticException e) {
//捕获异常
//打印异常信息
System.out.println("你除数是0了");
}
System.out.println("你猜猜我执行了吗");
执行了后面两句打印
/*
* 多Catch 处理异常
*/
public class demo03 {
public static void main(String[] args) {
/*
* 需求
* 处理空指针,角标越界,算术异常
*/
int[] array = new int[4];
try {
//报的越界异常 产生了越界异常对象
//new ArrayIndexOutOfBoundsException("5")
System.out.println(array[5]);
array = null;
System.out.println(array[1]);
System.out.println(10/0);
}catch (ArrayIndexOutOfBoundsException e) {
//在匹配Catch的时候有一个被匹配上,其他的Catch 就不会被匹配了
//如果Catch 中要写exceptin 来捕获异常 ,要放到最下面
System.out.println("你越界了");
}catch (NullPointerException e) {
System.out.println("空指针了");
}catch (ArithmeticException e) {
System.out.println("你除0 了");
}
System.out.println("你猜我执行了吗");
}
}
finally (遗言)
出现异常也不会影响到finally的执行 ,也就是说一定会去执行,通常用来关闭数据库,资源
private static void fun1() {
try {
System.out.println(10 / 2);
return;//结束这个方法
} catch (Exception e) {
System.out.println("你除零了");
} finally {
System.out.println("这是我的遗言,我还能行");
}
System.out.println("你猜我执行了吗");
}
class TestFianlly{
public int fun() {
int num = 10;
try {
num = 20;
System.out.println(10/0);
return num;
} catch (Exception e) {
//捕获异常
num = 30;
//建立了一个返回路径 相当于有一个箱子,箱子中保存的是要返回的值
// 不会马上return 会看一下有没有finally
// 有 finally 就执行一下 但是不会更改已经在返回路径里的值
//等finally 执行完 了在完成return
return num;
}finally {
num =40;
//return finally 中只写关闭资源 写return 会把之前的都覆盖掉
}
}
}
编译时的异常
比如
public static void main(String[] args) throws FileNotFoundException {
FileInputStream inputStream = new FileInputStream("wl.txt");
}
要读取该路径下的文件 ,系统不知道这个路径下是否有这个文件。此时编译异常
解决方法 : 1.添加 cry catch 来手动处理
2. 在main的声明上添加throws + 要抛出 的异常类名 就是相当于将异常抛给上级,这时候就和自己没关系 了
/*
* 需求:
* 创建一个人类 有name 和 age
* 要求 人类的年龄赋值时 要在 0 到 120岁之前
* 不用异常
*/
public class demo06 {
public static void main(String[] args) {
Preson preson = new Preson();
preson.setName("dp");
try {
preson.setAge(150);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(preson);
}
}
class Preson {
private String name;
private int age;
public Preson() {
super();
// TODO Auto-generated constructor stub
}
public Preson(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
//throws 关键字在方法的声明上标示这这个方法可能抛出异常
//谁调用这个方法异常就抛给谁了
public void setAge(int age) throws Exception {
//必须在这个范围内才给它赋值
if (age<=120 && age>=0) {
this.age = age;
}else {
//手动抛出一个异常
throw new AgeOutOfBoundsException("你是鬼仙人吗");
}
}
@Override
public String toString() {
return "[name=" + name + ", age=" + age + "]";
}
}
//创建自定义异常
class AgeOutOfBoundsException extends Exception{
/**
* 序列化时使用的id
*/
private static final long serialVersionUID = 1L;
public AgeOutOfBoundsException() {
super();
// TODO Auto-generated constructor stub
}
//需要写有参和无参的构造方法
public AgeOutOfBoundsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
//无限输入整数 存放到集合中 打印 输入quit停止
//希望在输入字符串的时候 让程序也能继续运行
public class demo09 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("请输入一个整数");
String string = scanner.nextLine();
if(string.equals("quit")) {
break;
}
try {
int num = Integer.parseInt(string);
list.add(num);
} catch (Exception e) {
System.out.println("改成数字吧傻比");
}
}
System.out.println(list);
}
}
异常处理
最新推荐文章于 2024-10-27 00:09:05 发布