You can see this post :
In particular it is now possible (and considered good practice) to set up a top-level exception handler that will handle any unexpected exception on the main thread in a Windows application. This means that it is no longer necessary to have exception handlers in every routine.
You can also see How to implement top-level exception handling?
And one link for exception handling in Java http://onjava.com/pub/a/onjava/2003/11/19/exceptions.html
So, as a general answer to your question: I would say that yes, it depends on the use case (itโs just your simple short script or a full-fledged application), but you should try to make an exception processing at the highest possible level, and remember about "technicality" of the message that you present to your users (believe me, the message "error 31231241 in the main thread" does not improve the user lines of your application).
edit Since Steve McConnell also states in his famous Code Complete 2 book , you need Throw exceptions on the right level of abstraction - for example if you have a getUser() method and you return IOException then that would be very bad. But yes, I think that is the meaning of commmon. In addition, he says that you need to write a function in such a way that if some other function sends it โgarbageโ, it should not cause the whole program to crash.
He also advocates the use of statements , and he says: Use error handling code for the conditions you expect to occur; use assertions for conditions that should never occur Use error handling code for the conditions you expect to occur; use assertions for conditions that should never occur .
Finally, it says that when addressing errors you should keep in mind two approaches: robustness and correctness . The story that he tells in the book for this example is very bright and remained in my head after I read it. Consider having a "text editing application" and consider the correctness of the data presented. Imagine that a few pixels "go into the wild" (you skipped their calculation or sth) - you probably wonโt want to close the application if something like this happens, and this is called reliability (continue to work). But imagine that you are making an application for processing x-ray images - in this case, any "supernatural data" should (according to McConnell) cause a critical error message, and they say that you are striving for correctness in your application.
PS pardon for the CC2 part, but I just love this book and I think that every developer should read it (at least once).
source share