Home arrow static arrow Java Programming [Archive] - Why use customized / particular exceptions instead java.lang.Exception
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Why use customized / particular exceptions instead java.lang.Exception
This topic has 12 replies on 1 page.

Posts:3
Registered: 7/30/04
Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 10:23 AM



 
Hi,

Do any of you guys know where can I find a theorical statement / explanation about why use particular / customized exceptions instead java.lang.Exception? I am aware that it consumes more resources and becomes a heavier object, as well as the clearness when coding and all that stuff; however, my boss wants to see a tech document where all this is clearly stated. Any resource over there?

Regards
 

Posts:31,095
Registered: 4/30/99
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 10:29 AM (reply 1 of 12)



 
There's the tutorial:

http://java.sun.com/docs/books/tutorial/essential/exceptions/
 

Posts:37,103
Registered: 3/30/99
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 11:38 AM (reply 2 of 12)



 
Do any of you guys know where can I find a theorical
statement / explanation about why use particular /
customized exceptions instead java.lang.Exception? I
am aware that it consumes more resources and becomes a
heavier object,

Huh? Why would using one exception class vs. another consume more resources?
 

Posts:1,511
Registered: 12/18/01
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:00 PM (reply 3 of 12)



 
Say you have a method that can have 3 different errors. For example (This is just theory, not actual code):

public void writeDocument(File file, String string) throws Exception{          BufferedWriter writer = new BufferedWriter(new FileWriter(file));                    if(string == null) throw  NullPointerException;                    writer.pintln("string");          }}


The above code can throw at least two kinds of Exceptions. However, if you just throw "Exception" you'll have to write up a little more code to see which one is thrown or actually look at the source to debug it (line numbers), yada yada yada.

If you want to error handle differently depending on the error thrown (different dialogs for example) you'll need to know which error was thrown. The only way to do that is to throw each error individually:

public void writeDocument(File file, String string) throws IOException, NullPointerException{          BufferedWriter writer = new BufferedWriter(new FileWriter(file));                    if(string == null) throw  NullPointerException;                    writer.pintln("string");          }}


Does that make sense?
 

Posts:1,511
Registered: 12/18/01
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:01 PM (reply 4 of 12)



 
Wrong code, sorry.

1st: example:
public void writeDocument(File file, String string) throws Exception{          BufferedWriter writer = new BufferedWriter(new FileWriter(file));                    if(string == null) throw  NullPointerException;          writer.pintln("string");}


2cnd:: example:

public void writeDocument(File file, String string) throws IOException, NullPointerException{          BufferedWriter writer = new BufferedWriter(new FileWriter(file));                    if(string == null) throw  NullPointerException;          writer.pintln("string");}
 

Posts:1,511
Registered: 12/18/01
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:03 PM (reply 5 of 12)



 
I'm assuming overhead caused by extending classes. At the worst, it would be miniscule, or so I would think.
 

Posts:105
Registered: 6/8/04
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:05 PM (reply 6 of 12)



 
When a block of code throws out multiple exceptions in different circumstances, you can catch them in different catch blocks and deal with them separately.

For example:

try{        tempInt = Integer.parseInt(myWeirdObject.toString());}catch(NumberFormatException e){        System.err.println(myWeirdObject.toString() + " isn't a valid number.");}catch(NullPointerException f){        System.err.println("myWeirdObject hasn't been instantiated.");}


Also, some Exception types can give more specific information about the error.
 

Posts:37,103
Registered: 3/30/99
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:07 PM (reply 7 of 12)



 
It is better to throw specific--or at least module- or package-specific--excpetions, rather than Exception, because then the caller knows what to expect. However, if you declare "throws Exception", the caller can still catch IOException, SQLException, etc., separately. He'll just have to be a really good guesser as to which ones he should catch.

Also, there's no point in declaring "throws NullPointerException." Any method can throw it without declaring it. If you generate your own NPE (or other unchecked exception) inside the method, then you should document it in the javadoc comments, but you don't need to put it in the throws clause. It doesn't do any harm, but it's redundant and cluttersome.
 

Posts:37,103
Registered: 3/30/99
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:08 PM (reply 8 of 12)



 
I'm assuming overhead caused by extending classes. At
the worst, it would be miniscule, or so I would think.

What overhead is created by extending classes?
 

Posts:1,511
Registered: 12/18/01
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:20 PM (reply 9 of 12)



 
It is better to throw specific--or at least module- or
package-specific--excpetions, rather than Exception,
because then the caller knows what to expect. However,
if you declare "throws Exception", the caller can
still catch IOException, SQLException, etc.,
separately. He'll just have to be a really good
guesser as to which ones he should catch.

True.

Also, there's no point in declaring "throws
NullPointerException." Any method can throw it without
declaring it. If you generate your own NPE (or other
unchecked exception) inside the method, then you
should document it in the javadoc comments, but you
don't need to put it in the throws clause. It doesn't
do any harm, but it's redundant and cluttersome.

It was an example and I wasn't feeling too creative....excuse me. ;-) But yeah, I've never thrown NPE ever.

I'm assuming overhead caused by extending classes.
At
the worst, it would be miniscule, or so I would
think.

What overhead is created by extending classes?

Hmm, I was under the impression that a class the extended another class, inherited all of the other classes data (variables, etc.). Hence, you get something like this:

SuperClass1 + SubClass1 + SubClassOfSubcCass1 = Memory usuage for SubClassOfSubClass1.

Since adding postivie number will always end up increasing data, SubClassOfSubClass1 will use more dtat than SuperClass1.

Am I wrong?
 

Posts:37,103
Registered: 3/30/99
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:44 PM (reply 10 of 12)



 
What overhead is created by extending classes?

Hmm, I was under the impression that a class the
extended another class, inherited all of the other
classes data (variables, etc.). Hence, you get
something like this:

SuperClass1 + SubClass1 + SubClassOfSubcCass1 = Memory
usuage for SubClassOfSubClass1.

Since adding postivie number will always end up
increasing data, SubClassOfSubClass1 will use more
dtat than SuperClass1.

Am I wrong?

If you add instance member variables, yes. But you don't usually do that when you extend Exception.

Although, you do add constructors, so those would add to the class definition's footprint, but that's a one-time hit per class, not per instance.

Miniscule indeed.
 

Posts:37,103
Registered: 3/30/99
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:45 PM (reply 11 of 12)



 
Since adding postivie number will always end up
increasing data, SubClassOfSubClass1 will use more
dtat than SuperClass1.

Am I wrong?

If you add instance member variables, yes.

"Yes, you're right," that is. Not, "Yes, you're wrong."
 

Posts:1,511
Registered: 12/18/01
Re: Why use customized / particular exceptions instead java.lang.Exception  
Jul 30, 2004 12:54 PM (reply 12 of 12)



 
Since adding postivie number will always end up
increasing data, SubClassOfSubClass1 will use more
dtat than SuperClass1.

Am I wrong?

If you add instance member variables, yes.

"Yes, you're right," that is. Not, "Yes, you're
wrong."

Ok, glad we are on the same page. Thanks for the extra info, too.

So, Conelly show your boss this thread.....it's on the web, how could it not be true? ;-)
 
This topic has 12 replies on 1 page.