In certain cases I want my function to simply crash and my app to stop execution:
public void foo() {
if(lifeIsWorthLiving) {
/*code ...*/
}
else {
/* log the problem, throw an exception and die */
}
}
Now if I add throw Exception("lifeIsWorthLiving = false !!!"); then I need to add throws Exception to function definition and then I have to handle this everywhere I call foo(). I don’t want to do that. How can I do this?
Throw an
RuntimeExceptionor maybeError, depending on the nature of the problem. Both doesn’t need to be declared in athrows. Last alternative is to callSystem#exit().