Ctrl + Ait + T
异常处理
package com.oop.demo01;
public class Outer {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
// System.out.println(a / b);
if(b == 0) {
throw new ArithmeticException();
}
} catch (Error e) {
System.out.println("Error!!!");
throw new RuntimeException(e);
} catch (Exception e) {
System.out.println("Exception!!!");
} catch (Throwable e) {
System.out.println("Throwable!!!");
} finally {
System.out.println("1111");
}
}
public static void a() {Outer.b();};
public static void b() {Outer.a();};
}
package com.oop.demo01;
public class Outer {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
divide(a, b);
} catch (ArithmeticException e){
e.printStackTrace();
}
}
public static void a() {Outer.b();};
public static void b() {Outer.a();};
public static void divide(int a, int b) throws ArithmeticException {
if(b == 0 ) {
throw new ArithmeticException();
}
}
}
自定义异常
package com.oop.demo01;
public class MyException extends Exception{
private int detail;
public MyException(int a) {
this.detail = a;
}
@Override
public String toString() {
return "MyException{" +
"detail=" + detail +
'}';
}
//toString
}
package com.oop.demo01;
public class Test {
static void test(int a) throws MyException{
System.out.println("argument: " + a);
if(a > 10) {
throw new MyException(a);
} else {
System.out.println("OK!!!");
}
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException + " + e);
}
}
}