异常
捕获异常
package com.exception;
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(0);
e.printStackTrace();
} finally {
}
}
}
package com.exception;
public class Test1 {
public static void main(String[] args) {
int a = 1;
int b = 0;
try {
if (b == 0) {
throw new ArithmeticException();
}
System.out.println(a / b);
}
catch (Error e) {
System.out.println("程序异常Error");
} catch (Exception e) {
System.out.println("程序异常Exception");
} catch (Throwable t) {
System.out.println("程序异常Throwable");
} finally {
System.out.println("finally");
}
}
}
抛出异常
package com.exception;
public class Test {
public static void main(String[] args) {
try {
new Test().tes(1, 0);
} catch (ArithmeticException e) {
e.printStackTrace();
} finally {
}
}
public void tes(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException();
}
System.out.println(a / b);
}
}