try catch
public static void main(String[] args) {
int a = 1;
int b = 0;
//Ctrl+Alt+T
try { //try 监控区域
System.out.println(a/b);
} catch (Error e) { //catch 捕获异常
System.out.println("Error");
} catch (Exception e) { //catch 捕获异常
System.out.println("Exception");
} catch (Throwable t) { //catch 捕获异常
System.out.println("Throwable");
} finally {//finally(可选) 处理善后工作
System.out.println("finally");
}
}
throw
public void test(int a, int b) {
if (b == 0) {
throw new ArithmeticException();
}
System.out.println(a/b);
}
throws
public void test(int a, int b) throws ArithmeticException {
System.out.println(a/b);
}