Solved

Java development throw exception in exception handler

Posted on 2010-11-13
7
711 Views
Last Modified: 2013-12-14
Hello,

Is it better to use multiple excepton handlers with throw options or handle exceptions without throw excpetion.Should one use throw exception at all ?

Best
bond77
0
Comment
Question by:bond7
7 Comments
 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 100 total points
ID: 34133378
I believe that wise old developers say that exceptions should only be thrown if the problem is caused by the user or the environment (and can be safely sidestepped or resolved), and should never be thrown if the programmer has simply failed to obey the method contract. (Though looking at the Java API, it seems that the JDK developers may not agree.)

It only makes sense to throw an exception to the client if there's a reasonable chance that the client can do something about it. If nothing can be done about the problem, and the problem makes it impossible to continue, then an error message should be generated and execution should terminate.

Whatever you do, don't simply catch an exception and then do nothing with it. Either throw it to the client, or die with an error message.
0
 
LVL 20

Expert Comment

by:a_b
ID: 34133493
Have a look the article below; that should answer your question.

http://onjava.com/pub/a/onjava/2003/11/19/exceptions.html
0
 

Author Comment

by:bond7
ID: 34133857
Hello,

Thanks for response.
I will elaborate on the question.
In a java program, I have a main method and another method say A.
I have a exception catcher at the method {Non main} and another excpetion catcher at main.
So i have two exception catchers.
I am wondering if I should throw exception at A , because it will be caught again by main method.
or I should just handle it at exception catcher at A.
0
 
LVL 20

Accepted Solution

by:
a_b earned 200 total points
ID: 34133993
That depends on your use case. You need one of the 2 exception handlers. Where you handle the exception depends on your use of the program. If it an exception that your code should handle (eg: a database connection issue or a null pointer exception) then you need to handle that in A otherwise if it is an exception that the cleint code (ie. main) should handle (eg wrong input) then handle that in main. Like I keep saying depends on the usecase.
0
 
LVL 2

Assisted Solution

by:jzkidding
jzkidding earned 200 total points
ID: 34134063
if the client can rectify the error  it is always better to throw the exception and inform the client about the error occurred. else catch the exception and write it into a log file for future analysis.

When ever an error has occurred and if it does not affect the flow then it has to be caught and logged
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 3 97
Adding multiple JVM environments to RedHat 6 7 23
Grunt script for Build Process 1 29
Syntax for query to update table 2 14
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

810 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