Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-15-2008, 08:40 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Better way to handle exceptions
I need your advice on this one. I wrote my own Exception class which as follows.

Code:
public class ParserException extends Exception { public ExtractorException(String msg){ super(msg); } }
It works perfectly fine. I have another option which is as follows:

Code:
public class ParserException extends Exception { private String expMsg; public ExtractorException(String msg){ this.expMsg = msg; } public String getExpMsg() { return expMsg; } public void setExpMsg(String expMsg) { this.expMsg = expMsg; } }
Please tell me which is the preferred way?

thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-15-2008, 11:15 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 263
roots is on a distinguished road
First one is better than second one.

You are defining your own message accessing methods even though there is already similar method in Exception Class. Normally exception messages are available during the creation of the exception message.
Further to that it requires the code that is catching this exception to be tightly coupled with this code meaning it needs to know how to get the message. In situation where this exception is caught by default handler such as JVM which called main method wont be able to show the message.

Second option has it's benefit as well but you should use it only when required for example you need to set the message down the exception chain.

Code:
catch(ParserException ex){ String rootMessage = ex.getExpMsg(); ex.setExpMsg(rootMessage + "SOME EXTRA DATA"); throw ex ; }
I wish other will contribute as well in this topic ..
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 08:47 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Thanks roots.
I will appreciate other responses as well.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Handle own exception stevemcc New To Java 3 04-10-2008 06:55 AM
How to handle socket Exception mayank0512 Networking 13 01-10-2008 02:53 PM
handle wrong input int/null Camden New To Java 1 12-16-2007 11:37 PM
how to handle exceptions paty Advanced Java 2 08-05-2007 06:17 AM
Some form to read pixels in Java and to handle Another plate VGA? Albert Advanced Java 1 06-07-2007 07:29 AM


All times are GMT +3. The time now is 11:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org