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
Solved

Java development throw exception in exception handler

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
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 …
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

840 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