java中如何让自定义异常像系统的那些异常一样不强制要求处理异常
PHPz
PHPz 2017-04-17 17:42:33
0
3
266

我在一个类的内部自定义了一个异常,然后在该类的构造方法中根据情况throw这个异常,但是IDE提示我必须要给方法加上throws,同时,在任何其他调用这个方法的地方,都强制要求使用try-catch或者在上级加上throws,我想让他像比如NullPointerException那样,不强制处理异常,而是直接报错终止程序,请问怎么实现?

PHPz
PHPz

学习是最好的投资!

reply all(3)
洪涛

You need to take a good look at the similarities and differences of ExceptionRuntimeException.

It’s very simple to realize your request

class MyException extends RuntimeException
Ty80
try{
//需要捕获异常的代码
} catch (Exception e) {
System.out.println(e.getMessage();
}

If you don’t want to throws, then capture and process them

左手右手慢动作

Then you throw it away and then capture it. After printing the error message, sysrem.exit() exits

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!