Exception和RuntimeException

时间:2023-03-09 01:35:13
Exception和RuntimeException

public class RuntimeExceptionDemo01 {
public static void main(String[] args) {
    String string="123";
    int temp=Integer.parseInt(string);
    System.out.println("123*123=: "+temp*temp);
}
}

 

    public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
    }

为什么main里不需要try catch?:

NumberFormatException extends IllegalArgumentException

IllegalArgumentException extends RuntimeException

RuntimeException extends Exception

可知:

1,Exception在程序中必须使用try。。。catch进行处理。

2,RuntimeException可以不使用try。。。catch进行处理,但如果有异常产生,则异常交由JVM进行处理。