我在一个类的内部自定义了一个异常,然后在该类的构造方法中根据情况throw这个异常,但是IDE提示我必须要给方法加上throws,同时,在任何其他调用这个方法的地方,都强制要求使用try-catch或者在上级加上throws,我想让他像比如NullPointerException那样,不强制处理异常,而是直接报错终止程序,请问怎么实现?
学习是最好的投资!
You need to take a good look at the similarities and differences of Exception 和RuntimeException.
Exception
RuntimeException
It’s very simple to realize your request
class MyException extends RuntimeException
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
You need to take a good look at the similarities and differences of
Exception
和RuntimeException
.It’s very simple to realize your request
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