4 ms·
If you don't care about exceptions just put, try { //your code } catch (Exception e) { throw new RuntimeException(e); } around it and you'll be fine. The th
by narrator 6y ago
If you don't care about exceptions just put,
try {
//your code
} catch (Exception e) {
throw new RuntimeException(e);
}
around it and you'll be fine.
The thing about Java is that the code lasts for a really long time because when it fails, it does so usually with an exception that points to the problem. When promises hang in NodeJS or memory corruption happens in C, it's much more annoying to track down problems.
- mumblemumble 6y agoYou can do that - I do it all the time myself - but I'd stop short of saying, "you'll be fine." That approach has its own downside, which is that it makes life obnoxious if you ever do want to actually handle exceptions. Then you'll have to add extra logic to determine if the exception you've caught is the actual error, or just a wrapper of some type. So your error handling inevitably becomes more error-prone. If you can get your team to commit to a "let it crash" policy, though, then it's pretty doable.
- koolba 6y agoYou’ll end up double wrapping your exceptions though so your stack traces get a bit uglier. Better to check if it’s a runtime exception and throw it again without wrapping it. Only wrap if it’s not a runtime exception. Also you should check if the thread was interrupted and, if so, set the interrupt flag again. The worst part of this is having that dance littered throughout all your code.
- bobthepanda 6y agoLombok has @SneakyThrows which does pretty much this but without the additional wrapping.
- elFarto 6y agoThere's also this magic function: private static <T extends Throwable> T sneakyThrow(Throwable ex) throws T { throw (T) ex; } Due to generic type erasure, the entire throw clause gets erased so it looks like this method doesn't throw anything.
- sedatk 6y agoOr, you can use C# and not put anything at all.