Good practice for exception handling


J

John Smith

When implementing exception handling which one of these two alternative
ways of exception-handling is the better one?

******************************************************
Alt1)
******************************************************
try
{
codeline1; //throws exception1, exception2 and exception3
codeline2; //throws exception4, exception5 and exception6
codeline3; //throws exception7, exception8 and exception9
codeline4; //throws exception10, exception11 and exception12
codeline5; //throws exception13, exception14 and exception15
}

catch(exception1){}
catch(exception2){}
catch(exception3){}
...............................
catch(exception14){}
catch(exception15){}
******************************************************
OR Alt 2)
******************************************************
try
{
codeline1; //throws exception1, exception2 and exception3
}
catch(exception1){}
catch(exception2){}
catch(exception3){}

try
{
codeline2; //throws exception4, exception5 and exception6
}
catch(exception4){}
catch(exception5){}
catch(exception6){}

try
{
codeline3; //throws exception7, exception8 and exception9
}
catch(exception4){}
catch(exception5){}
catch(exception6){}
******************************************************
 
Ad

Advertisements

G

Guest

What's the idea for Alt 2 case?
When implementing exception handling which one of these two alternative
ways of exception-handling is the better one?

******************************************************
Alt1)
******************************************************
try
{
codeline1; //throws exception1, exception2 and exception3
codeline2; //throws exception4, exception5 and exception6
codeline3; //throws exception7, exception8 and exception9
codeline4; //throws exception10, exception11 and exception12
codeline5; //throws exception13, exception14 and exception15
}

catch(exception1){}
catch(exception2){}
catch(exception3){}
...............................
catch(exception14){}
catch(exception15){}
******************************************************
OR Alt 2)
******************************************************
try
{
codeline1; //throws exception1, exception2 and exception3
}
catch(exception1){}
catch(exception2){}
catch(exception3){}

try
{
codeline2; //throws exception4, exception5 and exception6
}
catch(exception4){}
catch(exception5){}
catch(exception6){}

try
{
codeline3; //throws exception7, exception8 and exception9
}
catch(exception4){}
catch(exception5){}
catch(exception6){}
******************************************************

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Ad

Advertisements

D

David Levine

The real question is what are the requirements of the code? Does each
codeline need to be capable of recovering independently from the others or
do they share recovery paths? Is there even a recovery path? What are the
requirements for calling code; does it allow exceptions to bubble up or is
it defined as completely handling all exceptions within the code body? In
other words, the answer to your question is: it depends.
 

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

Similar Threads

Try Catch Exception 14

Top