Exception和RuntimeException

本文通过一个简单的Java程序示例,解释了运行时异常(RuntimeException)的处理方式。具体介绍了RuntimeException及其子类NumberFormatException如何在Java中被处理,包括为何在某些情况下可以不使用try...catch语句来捕获这些异常。

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进行处理。

转载于:https://www.cnblogs.com/vonk/p/3891674.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值