public class Demo01 {
public static void main(String[] args) {
int a=1,b=0;
//捕获多个异常时,要从小到大,向下写
//快捷键ctrl+alt+T
//异常被捕获,程序可以继续运行
//catch中填写的是想要捕获的异常类型
try {
// System.out.println(a/b);
new Demo01().test(a,2);
}catch (ArithmeticException e){
System.out.println(e);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
//主动抛出异常
public void test(int a,int b) throws Exception {
if(b==0)
System.out.println("hhhh");
else
System.out.println("aaa");
System.out.println(a/b);
}
}
package com.gaopeng.exception;
public class MyException extends Exception{
private int a;
public MyException() {
super();
}
public MyException(int a) {
this.a = a;
}
@Override
public String toString() {
return "MyException{" +
"a=" + a +
‘}‘;
}
}
package com.gaopeng.exception;
public class Test {
public static void main(String[] args){
try {
new Test().a();
} catch (MyException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
} finally {
}
System.out.println(1/0);
}
public void a() throws MyException {
throw new MyException(1);
}
}
原文:https://www.cnblogs.com/Gpengbolg/p/15270405.html