Solved

Java development throw exception in exception handler

Posted on 2010-11-13
7
714 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

734 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