异常
package com.exception.demo01;
public class Test {
public static void main(String[] args) {
new Test().test(1,0);
}
public void test(int a,int b) {
if (b == 0) {
throw new ArithmeticException();
}
System.out.println(a / b);
}
}
package com.exception.demo01;
public class Test2 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
System.out.println(a/b);
} catch (Exception e) {
System.exit(1);
e.printStackTrace();
} finally {
}
}
}
自定义异常
package com.exception.demo02;
public class MyException extends Exception{
private int detail;
public MyException(int a) {
this.detail = a;
}
@Override
public String toString() {
return "MyException{" + detail + '}';
}
}
package com.exception.demo02;
public class Test {
static void test(int a) throws MyException {
System.out.println("传递的参数为:"+a);
if (a>10){
throw new MyException(a);
}
System.out.println("OK");
}
public static void main(String[] args) {
try {
test(11);
} catch (MyException e) {
System.out.println("MyException=>"+e);
}
}
}