when to throws or catch exception in chains of method calls?

M

Matt

I want to know what is the best practice to throw or catch exception
in
chains of method calls. Consider the following cases with method
call sequence: f->g->h->i. All the following approaches will work, but
want to know which is the best approach. Please advise. thanks!!

=====================================================
Approach #1: declare try/catch block only in FIRST CALLING METHOD,
which is
method f. Method f will catch all exceptions that method f, g, h will
thrown.
Method f don't throw any exception since method f is FIRST CALLING
METHOD.
And the rest will just throw the exception.
=====================================================

void f()
{ try
{ //code
g();
}
catch(exception0)
{ //handle its own method f's exception0
}
catch(exception1)
{ //handle method g's exception1
}
catch(exception2)
{ //handle method h's exception2
}
}

//method g throws exception1, and exception2 is for method h
void g() throws exception1, exception2
{ //code
h();
}

//method h throws exception2
void h() throws exception2
{ //code
i();
}

//method i no exception thrown
void i()
{ //code
}

===========================================
Approach #2: just catch its own exception on each method, no need to
take
care other methods' exception because exception already be catched.
============================================
//method f catch its own exception0, no need to take care of other
methods' exceptions
void f()
{ try
{ //code
g();
}
catch(exception0)
{ //handle its own method f's exception0
}
}

//method g catch its own exception1, no need to take care of method
h's exception2
void g()
{ try
{ //code
h();
}
catch(exception1)
{ //handle exception1
}
}

//method h catch its own exception2
void h()
{ try
{ //code
i();
}
catch(exception2)
{ //handle exception2
}
}

//method i has no exception
void i()
{ //code
}

=================================================
Approach #3: throws exception and catch exception for each method that
can throw exception
==================================================

void f() throws exception1, exception2
{ try
{ //code
g();
}
catch(exception0)
{ //handle its own method f's exception0
}
catch(exception1)
{ //handle method g's exception1
}
catch(exception2)
{ //handle method h's exception2
}
}

//method g throws exception1, and exception2 is for method h
void g() throws exception1, exception2
{ try
{ //code
h();
}
catch(exception1)
{ //handle exception1
}
catch(exception2)
{ //handle exception2
}
}

//method h throws exception2
void h() throws exception2
{ try
{ //code
i();
}
catch(exception2)
{ //handle exception2
}
}

//method i no exception thrown
void i()
{ //code
}
 
T

Tony Morris

I strongly suggest you read before posting to this newsgroup.
Also, I believe that there are other newsgroups more suited for trivial
questions (I don't really care - others might).

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

Did mention?
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform
 
C

Chris Smith

Matt said:
I want to know what is the best practice to throw or catch exception
in
chains of method calls. Consider the following cases with method
call sequence: f->g->h->i. All the following approaches will work, but
want to know which is the best approach. Please advise. thanks!!

What are you doing when you "handle" an exception? Do you need to
resume processing afterward, or retry something, or just abort it all?
You should catch the exception in the place that's appropriate to do so.
For example, if you want to abort all of f() whenever an exception
occurs, then you probably want to catch all of your exceptions in f...
otherwise, you'd need to work out some other way of unwinding the stack
for the aborted operation. On the other hand, if you can fix something
and retry h, then you probably want to catch h's exception in g, where
you can immediately retry the operation and continue processing.

There's no possible way that anyone can give a correct answer to your
question about which is "best", without understanding your business
logic. That's kinda like asking whether int or float is the best data
type for a variable, without bothering to say what the variable will be
used for.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top