Solved

Java development throw exception in exception handler

Posted on 2010-11-13
7
708 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now