Originally posted by Kaush Kane:
I have one general question on this topic.
Normally it is said that the code which is likely to throw exception shuould be inside the try block.
The question I have is: In case of Java Runtime Exceptions it is not possible everytime to know which part of code wud throw an exception. In such case how to decide that which part of the code shud be in try block.
That's only partially true. Exceptions a method may throw should be documented regardless of whether or not they are checked or unchecked. If you and your colleagues do this then all that is needed to find out what can be thrown is a check of the documentation. In that scenario it's purely a matter of whether or not you want to handle the exception there. If you can handle it and want to handle it there, then do so. If you can guarantee the exception will never occur that's antoher option. If you do want to handle it, then handle it.
Working with legacy code or APIs outside of your control is much more difficult if the developer did not document exceptions well. In that case I'm not sure there's much you can do except deal with it on a case by case basis. As Stan alluded to some type of safety net to catch anything that slips through which should not take down the entire application is also a good idea. Hopefully it never reaches that point, but better to have the problem logged for you to deal with than to have the entire application die for something trivial.