?
Solved

Java development throw exception in exception handler

Posted on 2010-11-13
7
Medium Priority
?
737 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
5 Comments
 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 400 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 800 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 800 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

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

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Strategic internal linking is often considered an SEO power technique, especially for content marketing. Do you need to hire an SEO agency to optimize you internal linking? No, this article will help you understand the basics of internal linking and…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

864 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