vis kat wrote:Hi guyz,
I have a situation in a java program where I want to exclude a statement from the exception trap.
Below is the example that will make things clear :
--------------------------------------------------------------------------------
(Changed your code a bit to make my explanation clearer.)
If you do that, you're saying that you want method() to execute even if #1 fails. That's not generally a good idea. The only way that makes any sense is if, when #1 throws an exception, you are able to recover from it and do its job in some other fashion, (such as falling back on some file in a default location you know will always be available if you're unable to open a file at a preferred location), or if what it's doing is something "extra" that you can live without. If this is the case, you can split it up, as Jesper shows.
However, I fear you may be suffering from a common misconception that exceptions are just just a natural occurrence and try/catch was put in the language to get them out of your way, or that catch somehow fixes the error. Don't fall into that trap. The exception mechanism exists so that you can separate your "happy path" code from your error handling code. They don't fix any errors though. When something goes wrong, you have to either fix it (by catching and actually
handling the exception, or you have to let the caller know that it went wrong--that you were unable to to what he asked you to do (by not catching in the first place, or by rethrowing).