3 ms·
The best part about exceptions (unchecked), is that they allow an exception to automatically bubble upward to the level of code that actually should be responsi
by srgseg 15y ago
The best part about exceptions (unchecked), is that they allow an exception to automatically bubble upward to the level of code that actually should be responsible for taking action, without having to write a ton of boilerplate code all the way down the call chain.
Even better, when stack traces in exception logs are examined, it's easy to see where in the call chain things are going wrong.
During a database transaction for example, something might go wrong 5 method calls deep, and I should roll back the transaction.
If I'm forced to either use checked exceptions or use if statements to check whether there was success at each level, then that's a lot of repeated boilerplate try/catch/rethrow or if(success) code which unchecked exceptions free me up from having to write.
If all I receive at the transaction level is a failure code, then what do I log, other than "something went wrong?!". With exceptions, all the work is done for me. I just log the exception stack trace and I can easily see what needs to be fixed.
Much of the time, exceptions are useful because they are thrown becuase of something you didn't anticipate happening, as opposed to something you had planned for when writing the code. And when that unanticipated thing does happen, there is nothing more beautiful than an automatically generated stack trace telling you exactly what happened.