[LC++]exception handling

Jan Pfeifer pfjan at yahoo.com.br
Thu Dec 9 22:04:02 UTC 2004


Hi Mehul,

there is no right and wrong in this case (*). It depends where you want
to handle the error/exception. I'll try to give you a short explanation
on exceptions, sorry if it is already obvious to you.

think of exceptions as exceptional, typically error, conditions, that a
method/function/block of code don't know how to handle, or just prefers
to leave to some other piece of code to handle. 

instead of creating cumbersome parameter (return value) manipulation to
indicate this error condition, you can "throw an exception". Usually
methods/functions documentations lists the possible exceptions it may
generate. 

the code using the method/function can then handle the exceptions as
they see fit, or just leave to some other code to handle. If nothing
"catches" an exception, it will cause your program to exit.

so if you want for your function "div" to know how to handle the
exceptional condition, just use the second code. Note that in this case,
the calling code will never know about the exception/error.

generally if your developing a library, i would recommend the first
form, where the user of the library gets to decide what to do when some
error occurs.

note that if you're using exceptions inside a more complex
method/function, before leaving the method because of an exception, you
might have to free allocated resources (memory, close files, and so on).
A commom method to implement this is to code a catch in the end of your
method that frees the allocated resources and re-throws the exception.

there is little speed difference among the two forms. 

hope this helps :)

jan


(*) I haven't tested your code, but I noticed is that you throw a "const
char *" and you're catching a "string", which are different types, so it
might not work. And if you are using simple throws, try to use the
standard exception classes already defined -- runtime_error,
logical_error, and so on :)





On Thu, 2004-12-09 at 11:02 +0000, choube mehul wrote:
> Hello all;
> 
> please look at the following code:
> 
> Code 1:
> void div (int n1, int n2)
> {
>   if (n2 == 0) {
>     throw "divide by zero";
>   }
> 
>   cout << n1/n2 << endl;
> }
> 
> int main (int argc, char argv)
> {
>   int n1 = 10, n2 = 0;
> 
> 
>   try {
>     div (n1, n2);
>   } catch (string msg) {
>     cout << msg << endl;
>   }
> 
>   return 0;
> }
> 
> Code 2:
> void div (int n1, int n2)
> {
>   try {
>     if (n2 == 0) {
>       throw "divide by zero";
>     }
> 
>     cout << n1/n2 << endl;
>   } catch (string msg) {
>     cout << msg << endl;
>   }
> }
> 
> int main (int argc, char argv)
> {
>   int n1 = 10, n2 = 0;
> 
>   div (n1, n2);
> 
>   return 0;
> }
> 
> which one from the above is correct
> Code 1 or Code 2?
> 
> which is one is the general practice/efficient?
> 
> what are the advantages, disadvantages?
> 
> thanks in advance.
> 
> mehul.
> 
> 
> ________________________________________________________________________
> Yahoo! India Matrimony: Find your life partner online
> Go to: http://yahoo.shaadi.com/india-matrimony
> 
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
> 
-- 
Jan Pfeifer <pfjan at yahoo.com.br>





More information about the tuxCPProgramming mailing list