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
}
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
}