Contents
Can we handle exception without try catch?
throws: Throws keyword is used for exception handling without try & catch block. It specifies the exceptions that a method can throw to the caller and does not handle itself.
Is it bad practice to catch exception?
catch(Exception) is a bad practice because it catches all RuntimeException (unchecked exception) too. This may be java specific: Sometimes you will need to call methods that throw checked exceptions. If this is in your EJB / business logic layer you have 2 choices – catch them or re-throw them.
Is it bad to use Pokemon exception handling?
Pokemon exception handling (gotta catch em all!) is certainly not always bad. When your exposing a method to a client, especially an end user its often better to catch anything and everything rather than have your application crash and burn. Generally though they should be avoided where you can.
Is it possible to catch all possible exceptions?
The two possibilities are not mutually exclusive. In an ideal situation, you would catch all possible types of exception your method could generate, handle them on a per-exception basis, and in the end add a general catch clause to catch any future or unknown exceptions. This way you get the best of both worlds.
What does it mean to catch an exception and log it?
When you’re specific, it means that you actually understand, more concretely, what your application is doing, and have more control over it. In general, if you come upon a situation where you just catch an Exception, log it and continue, there’s probably some bad things that are going on anyway.
When to catch a java.lang.exception?
Catching java.lang.Exception can be legitimate in specific cases, but generally “I’m lazy” is not one of them. 🙂 For example if you’re implementing an API and want to make absolutely sure no exception ever comes out of it that isn’t in the specification, you might catch Exception and wrap it in some application exception you’ve defined.