4 ms·
try { *your stuff* } catch (WhateverYouDontWantToHandleException e) { throw new RuntimeException(e); } Then at the outmost level of your code som
by sfjailbird 13y ago
try {
*your stuff*
} catch (WhateverYouDontWantToHandleException e) {
throw new RuntimeException(e);
}
Then at the outmost level of your code somewhere you catch RuntimeException and tell the user a general error occurred or something. You will still get the root exception so you can log with stack trace and eventually implement some specific exception handling for it if necessary.
- StavrosK 13y agoIsn't this the same as "public void myFunction throws WhateverYouDontWantToHandleException" (i.e. letting it propagate up implicitly)?
- sfjailbird 13y agoNo, that will force whoever is calling your method to handle that exception, now without knowing the actual context ("why does getWeatherForecast() throw MalformedURLException?" etc.). It also leaks an implementation detail, i.e. the use of URL-whatever. A RuntimeException, and any subclass of it, is unchecked and so the compiler does not force you to handle it.
- StavrosK 13y agoOh, right, you convert it to RuntimeException (ostensibly in multiple places where you don't want to handle the exception) and catch that one as a general exception. It makes sense now and sounds like a good solution, thank you.
- tomjen3 13y agoIt is a horrible solution, but it does work. IMHO, go was right to get rid of exceptions (stack traces are useful) and just return two values.