[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-04-29
9
Medium Priority
?
290 Views
Last Modified: 2012-05-11
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!
0
Comment
Question by:php-newbie
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 35491009
>>I'm trying to figure out how I know which exceptions to catch. I read that I should catch all exceptions.

That's not the case. There are two categories of exception - checked and unchecked. The latter should rarely be caught and the former are always caught (if only by the runtime default handler)

http://download.oracle.com/javase/tutorial/essential/exceptions/runtime.html
0
 
LVL 3

Expert Comment

by:Mrugesh1
ID: 35491042
catch(Exception e)
{  
    e.getMessage();
}
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 35491231
>> 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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:php-newbie
ID: 35491254
So for the checked exceptions, I need to look up every method in the documentation and find out which exception they might throw?
0
 
LVL 23

Assisted Solution

by:cmalakar
cmalakar earned 1000 total points
ID: 35491264
>> Do I need to read the Java documentation for every method I use and find out which exceptions it might catch?

Better check the documentation for any Predefined class that you use (either from core java sdk or any library)
and catch the exceptions as required. Meaning, When you need to handle any special cases you need to catch specific exception.
Else, catch "java.lang.Exception"

For example, FileReader throws FileNotFoundException, if the file being passed doesnot exist.
If your app is expected to throw this specific error to user, then catch this specific exception and handle it accordingly.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 35491309
The compiler will tell you which exceptions you're failing to catch
0
 
LVL 23

Assisted Solution

by:cmalakar
cmalakar earned 1000 total points
ID: 35491343
If you use IDE's like eclipse/Netbeans, they will tell you which exceptions to catch.. as you write code.
0
 

Author Closing Comment

by:php-newbie
ID: 35491789
Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35492063
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month20 days, 12 hours left to enroll

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question