Link to home
Start Free TrialLog in
Avatar of Jax Logan
Jax Logan

asked on

How do I know which exceptions to catch (java)?

Experts,

I'm new to Java so bear with me.

I'm trying to figure out how I know which exceptions to catch. I read that I should catch all exceptions.

Do I need to read the Java documentation for every method I use and find out which exceptions it might catch?

Do I need to enclose all methods that throws an exception in to a separate try/catch block. Seems like a lot of work or is there an easier way...?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mrugesh1
Mrugesh1

catch(Exception e)
{  
    e.getMessage();
}
>> Do I need to enclose all methods that throws an exception in to a separate try/catch block. Seems like a lot of work or is there an easier way...?

If you catch the parent of all checked exceptions "java.lang.Exception"
all thrown exceptions will be caught in that catch handler.
Avatar of Jax Logan

ASKER

So for the checked exceptions, I need to look up every method in the documentation and find out which exception they might throw?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!
:)