best pratices in Exception Handling

  • Thread starter philippe.barthelemy_GOOGLE
  • Start date
P

philippe.barthelemy_GOOGLE

Hi

I am a rookie Java developer.
I am looking for a guide on design and architecture in Exception
generation and handling.

I have found a lot of tutorial on the syntax, but not one on how to
actually design a sensible Exception handling.

<rant>
( the 'think in Java' book is a farce, it should be named 'write the
correct syntax in Java', for instance...)
</rant>

anyone can direct me to a good tutorial ?
TIA,
--philippe
 
C

Chris Smith

I am a rookie Java developer.
I am looking for a guide on design and architecture in Exception
generation and handling.

I have found a lot of tutorial on the syntax, but not one on how to
actually design a sensible Exception handling.

For some general concepts, head over to www.ppjdg.org, click "Java
Essentials" in the left margin, and then "Exception Handling Practices"
from the resulting list. That's the slide piece to a 30-minute
presentation I prepared about a year ago on the topic.

I think you're making a mistake, though, in looking for architectural
advice regarding exception handling. The mechanism is already there.
All you need to do is be sure that you're throwing and catching the
right exceptions at the right times. If you try to architect your
exception handling in the common sense of that word, you're going to
create unneeded complexity and make it harder for others to approach
your code.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
B

barthelemy

Thanks for this, it is pretty helpful.
I guess you're right, I won't think too much ....

thanks,
--Philippe
 
R

Rex Guo

I have found a lot of tutorial on the syntax, but not one on how to
The quickest and shortest advice I can
offer you are these:

1. Handle your exception as close to where it is
thrown as possible. Handling it late, that is,
far down the call chain, has the same effect
as procrastination. :)

2. Reuse Java's built-in exception classes as much
as possible because they already cover most of
what you will ever need. Example is the
IllegalArgumentException when checking a your cool
function's parameters.

3. Create a new exception class to handle errors
specific to your application. This way you can
differentiate errors originating from your app
from (common) low-level errors with using Java
itself. Example: handling a file-not-found error
is quite different from handling a customer-entered-
invalid-application-form kind of errors.

4. Have a try-catch block around your main function
as a last line of defense (before it goes to the
JVM for crashing), because you can at least do
something useful when all else fails, such as
saving critical data, or even saving the stack
trace for your user to send to you! :)

Good luck!

..rex
 
S

steve

Hi

I am a rookie Java developer.
I am looking for a guide on design and architecture in Exception
generation and handling.

I have found a lot of tutorial on the syntax, but not one on how to
actually design a sensible Exception handling.

<rant>
( the 'think in Java' book is a farce, it should be named 'write the
correct syntax in Java', for instance...)
</rant>

anyone can direct me to a good tutorial ?
TIA,
--philippe

you need to look at your code.

basically consider the following:
( bad example)

you have a routine that performs some function: A

Routine B calls A
Routine C calls B

you put an exception handler in A,B,C.

your routine A causes an exception, and you catch it.
because your data is bad from A, it causes an exception in B & you catch it
Because your data is bad from B it causes an exception in C. & you catch it

Whilst this is expected, it will cause your exception to "chain" up the
execution stack, but more importantly ,if this is a GUI application , it can
cause complete failure of the program.


sometimes it is better NOT to catch an exception at the place it is caused,
but to pass it back up the execution chain, using the "Throws Exception"
keyword.


But you SHOULD always catch & report ALL EXCEPTIONS, do not ever use empty
exception blocks.
I have even seen tutorials & java ref books doing this!!.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top