What do you mean "return" an exception? When an exception is thrown, it bubbles up the call stack.
You are not handling it in this case. It reaches main
and thus you have an unhandled exception.
If you want to handle an exception, you'd use a try-catch
block. Preferably surrounding main
in this case.
try {
// Code that might throw
// an exception.
} catch (Exception e) {
// Handle it.
}
Another solution would be to specify that main
throws a "RandomWeirdException
", and not catch
it in the first place.
public static void main(String[] args) throws RandomWeirdException { ... }
It's preferable to just let functions throw
, unless you can reasonably handle the exceptional case.
If you just catch
for the sake of catching without doing anything meaningful in an exceptional case, it's the equivalent of hiding an error sometimes.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…