Basic question on exception handling in C++

Discussion in 'C++' started by masood.iqbal@lycos.com, Jun 12, 2007.

  1. Guest

    In all the sample code snippets of try-catch code blocks that I have
    seen, the catch block does one of the following three things:
    1). exits the program (after spitting out a cerr message)
    2). propagates the exception
    3). throws yet another exception

    I have a need to do something different. I want to merely spit out a
    cerr message when I catch an exception, and then proceed with my
    business logic. I am thinking of something like this:

    ////// Code snippet begin /////
    bool noException = true;

    try
    {
    // some business logic operation
    }
    catch(std::exception& xcptn)
    {
    noException = false;
    cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
    << __LINE__ << endl;
    cerr << "Ignoring business logic operation\n";
    }
    catch(...)
    {
    noException = false;
    cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
    << endl;
    cerr << "Ignoring business logic operation\n";
    }


    if(noException)
    {
    // proceed with this business logic operation
    }

    ////// Code snippet end /////

    Am I doing it right, or is there a more professional way to do it?


    Thanks,
    Masood
     
    , Jun 12, 2007
    #1
    1. Advertising

  2. On 12 Juni, 15:04, wrote:
    > In all the sample code snippets of try-catch code blocks that I have
    > seen, the catch block does one of the following three things:
    > 1). exits the program (after spitting out a cerr message)
    > 2). propagates the exception
    > 3). throws yet another exception
    >
    > I have a need to do something different. I want to merely spit out a
    > cerr message when I catch an exception, and then proceed with my
    > business logic. I am thinking of something like this:
    >
    > ////// Code snippet begin /////
    > bool noException = true;
    >
    > try
    > {
    > // some business logic operation}
    >
    > catch(std::exception& xcptn)
    > {
    > noException = false;
    > cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
    > << __LINE__ << endl;
    > cerr << "Ignoring business logic operation\n";}
    >
    > catch(...)
    > {
    > noException = false;
    > cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
    > << endl;
    > cerr << "Ignoring business logic operation\n";
    >
    > }
    >
    > if(noException)
    > {
    > // proceed with this business logic operation
    >
    > }
    >
    > ////// Code snippet end /////
    >
    > Am I doing it right, or is there a more professional way to do it?


    Seems good to me. If you wish you can put the business logic operation
    within the try-block, after the thing that might throw, since no more
    statements will be executed in the try block if an exception is
    thrown.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jun 12, 2007
    #2
    1. Advertising

  3. Marcus Kwok Guest

    In comp.lang.c++ Erik Wikström <> wrote:
    > On 12 Juni, 15:04, wrote:
    >> ////// Code snippet begin /////
    >> bool noException = true;
    >>
    >> try
    >> {
    >> // some business logic operation}
    >>
    >> catch(std::exception& xcptn)
    >> {
    >> noException = false;
    >> cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
    >> << __LINE__ << endl;
    >> cerr << "Ignoring business logic operation\n";}
    >>
    >> catch(...)
    >> {
    >> noException = false;
    >> cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
    >> << endl;
    >> cerr << "Ignoring business logic operation\n";
    >>
    >> }

    >
    > Seems good to me. If you wish you can put the business logic operation
    > within the try-block, after the thing that might throw, since no more
    > statements will be executed in the try block if an exception is
    > thrown.


    One thing that I noticed is that your (the OP's) cerr output will always
    output the same line number in the message, that is, the line number of
    the cerr output statement. If you want to really see where the
    exception occured, you need to encode it into the exception somehow at
    the throw point.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Jun 12, 2007
    #3
  4. On 12 Juni, 19:45, (Marcus Kwok) wrote:
    > In comp.lang.c++ Erik Wikström <> wrote:
    >
    >
    >
    > > On 12 Juni, 15:04, wrote:
    > >> ////// Code snippet begin /////
    > >> bool noException = true;

    >
    > >> try
    > >> {
    > >> // some business logic operation}

    >
    > >> catch(std::exception& xcptn)
    > >> {
    > >> noException = false;
    > >> cerr << "Exception: " << xcptn.what() << " at " << __FILE__ << ","
    > >> << __LINE__ << endl;
    > >> cerr << "Ignoring business logic operation\n";}

    >
    > >> catch(...)
    > >> {
    > >> noException = false;
    > >> cerr << "Unrecognized exception at " << __FILE__ << "," << __LINE__
    > >> << endl;
    > >> cerr << "Ignoring business logic operation\n";

    >
    > >> }

    >
    > > Seems good to me. If you wish you can put the business logic operation
    > > within the try-block, after the thing that might throw, since no more
    > > statements will be executed in the try block if an exception is
    > > thrown.

    >
    > One thing that I noticed is that your (the OP's) cerr output will always
    > output the same line number in the message, that is, the line number of
    > the cerr output statement. If you want to really see where the
    > exception occured, you need to encode it into the exception somehow at
    > the throw point.


    Yes, I've been searching for a good solution to this but the best I've
    come up with was declaring an exception-class like this:

    class myException : public std::exception
    {
    myException(const char* msg, const char* file, long line);
    };

    And using a macros like this:

    #define myException(x) myException(x, __FILE__, __LINE__)

    So when I throw an exception I use the macro like this:

    throw myException("Illegal value");

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jun 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Satterwhite

    (Very) Basic question re: exception

    Michael Satterwhite, Apr 21, 2004, in forum: C++
    Replies:
    6
    Views:
    489
    John Carson
    Apr 21, 2004
  2. Vijay
    Replies:
    7
    Views:
    383
    James Kanze
    Jun 30, 2008
  3. Handy

    Basic Exception Handling

    Handy, Apr 13, 2009, in forum: C Programming
    Replies:
    0
    Views:
    310
    Handy
    Apr 13, 2009
  4. Peter
    Replies:
    34
    Views:
    1,975
    James Kanze
    Oct 17, 2009
  5. VSK
    Replies:
    0
    Views:
    255
Loading...

Share This Page