catching assertion error

Discussion in 'C++' started by Eric, Nov 7, 2006.

  1. Eric

    Eric Guest

    is it possible to catch an assertion error? that is, could the following
    block of code be made to run?

    assert (false);
    catch (...) {}
    Eric, Nov 7, 2006
    #1
    1. Advertising

  2. Eric

    mlimber Guest

    Eric wrote:
    > is it possible to catch an assertion error? that is, could the following
    > block of code be made to run?
    >
    > assert (false);
    > catch (...) {}


    No. Asserts are different from exceptions, and if you want to catch,
    you must throw.

    Cheers! --M
    mlimber, Nov 7, 2006
    #2
    1. Advertising

  3. Eric wrote:

    > is it possible to catch an assertion error? that is, could the following
    > block of code be made to run?
    > assert (false);
    > catch (...) {}


    The standard assert just prints some information and calls abort. Of course
    you can write your own assert macro and not include the standard header,
    but is clearer to use another name, such as ASSERT, MY_PROJECT_ASSERT... or
    better yet, don't use in the name nothing similar to assert if you want to
    use it as an exception and not as an assertion. For example, something like
    LOGIC_CHECK that throws std::logic_error if failed looks reasonable to me.

    --
    Salu2
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Nov 7, 2006
    #3
  4. Eric

    Salt_Peter Guest

    Eric wrote:
    > is it possible to catch an assertion error? that is, could the following
    > block of code be made to run?
    >
    > assert (false);
    > catch (...) {}


    No, if you want to catch, someone has to throw the ball (or whatever).

    #include <iostream>
    #include <stdexcept>

    int main()
    {
    try
    {
    throw std::logic_error( "testing logic_error" );
    }
    catch ( const std::exception& r_e )
    {
    std::cerr << "error: " << r_e.what() << std::endl;
    }
    }

    /*
    error: testing logic_error
    */

    Can you see why its a bad idea to catch(...) as opposed to a specific
    catch?
    Salt_Peter, Nov 7, 2006
    #4
  5. Eric

    VJ Guest

    Salt_Peter wrote:
    >
    > Can you see why its a bad idea to catch(...) as opposed to a specific
    > catch?
    >


    Why is it a bad idea to catch all?
    It has some good uses
    VJ, Nov 8, 2006
    #5
  6. Eric

    mlimber Guest

    VJ wrote:
    > Salt_Peter wrote:
    > >
    > > Can you see why its a bad idea to catch(...) as opposed to a specific
    > > catch?
    > >

    >
    > Why is it a bad idea to catch all?
    > It has some good uses


    He's not suggesting it's *always* a bad idea to catch all exceptions,
    but in the example he gave, it is better to catch a specific exception
    (or rather, a specific class of exceptions).

    Cheers! --M
    mlimber, Nov 8, 2006
    #6
  7. Eric

    Salt_Peter Guest

    VJ wrote:
    > Salt_Peter wrote:
    > >
    > > Can you see why its a bad idea to catch(...) as opposed to a specific
    > > catch?
    > >

    >
    > Why is it a bad idea to catch all?
    > It has some good uses


    Consider the list of possible standard exceptions that might be thrown:
    ios_base::failure
    bad_alloc
    bad_cast
    logic_error
    runtime_error
    bad_exception

    It would be a shame to catch any of these in a carch-all for obvious
    reasons.

    #include <iostream>
    #include <stdexcept>

    int main()
    {
    try {
    // may throw
    }
    catch ( const std::logic_error & e ) {
    std::cerr << "a logic_error was caught: ";
    std::cerr << e.what() << std::endl;
    }
    catch ( const std::exception & e ) {
    std::cerr << "a specific error was caught: ";
    std::cerr << e.what() << std::endl;
    }
    catch ( ... ) {
    std::cerr << "an undetermined error occurred\n";
    }
    }
    Salt_Peter, Nov 8, 2006
    #7
    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. Cormac
    Replies:
    3
    Views:
    2,550
    Kevin Goodsell
    Apr 6, 2004
  2. Kapt. Boogschutter

    Debug Assertion Failed! and no obvious Error

    Kapt. Boogschutter, Jun 23, 2004, in forum: C++
    Replies:
    1
    Views:
    577
    red floyd
    Jun 23, 2004
  3. assertion error

    , Jul 22, 2004, in forum: Python
    Replies:
    0
    Views:
    349
  4. ragu
    Replies:
    0
    Views:
    638
  5. Pushpa
    Replies:
    2
    Views:
    358
    Sarath
    Mar 9, 2007
Loading...

Share This Page