try-catch usage

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vmnvmcxbv

    try-catch usage

    Why does this:

    try
    {
    throw 1;
    }
    catch(...)
    {
    handle_exceptio n();
    }

    works as expected (exception is caught in the catch block)

    and this:

    try
    {
    throw;
    }
    catch(...)
    {
    handle_exceptio n();
    }

    crashes application? Isn't catch(...) supposed to catch _everything_?

    FWIW - I use VC++ 2008


  • Obnoxious User

    #2
    Re: try-catch usage

    On Tue, 26 Aug 2008 12:01:20 +0000, vmnvmcxbv wrote:
    Why does this:
    >
    try
    {
    throw 1;
    }
    catch(...)
    {
    handle_exceptio n();
    }
    >
    works as expected (exception is caught in the catch block)
    >
    and this:
    >
    try
    {
    throw;
    I doubt this statement is even allowed here. It is used within
    catch handlers to rethrow the exception so it can be catched
    again somewhere.

    --
    OU

    Comment

    • Ali Karaali

      #3
      Re: try-catch usage

      On 26 Aðustos, 15:01, "vmnvmcxbv" <ruyeir...@fhsd khf.netwrote:
      Why does this:
      >
      try
      {
          throw 1;}
      >
      catch(...)
      {
          handle_exceptio n();
      >
      }
      >
      works as expected (exception is caught in the catch block)
      >
      and this:
      >
      try
      {
          throw;}
      >
      catch(...)
      {
          handle_exceptio n();
      >
      }
      The catch can catch everything but you don't throw anything.
      So calling std::terminate( ) function

      Comment

      • red floyd

        #4
        Re: try-catch usage

        On Aug 26, 5:01 am, "vmnvmcxbv" <ruyeir...@fhsd khf.netwrote:
        Why does this:
        >
        try
        {
            throw 1;}
        >
        catch(...)
        {
            handle_exceptio n();
        >
        }
        >
        works as expected (exception is caught in the catch block)
        >
        and this:
        >
        try
        {
            throw;}
        >
        catch(...)
        {
            handle_exceptio n();
        >
        }
        >
        crashes application? Isn't catch(...) supposed to catch _everything_?
        >
        Per 15.1/8, "If no exception is presently being handled, executing a /
        throw-expression/ with no operand calls terminate()."

        In other words, a "throw;" must occur inside a catch block, or you
        will be terminated.

        Comment

        • Christian Hackl

          #5
          Re: try-catch usage

          red floyd wrote:
          Per 15.1/8, "If no exception is presently being handled, executing a /
          throw-expression/ with no operand calls terminate()."
          >
          In other words, a "throw;" must occur inside a catch block, or you
          will be terminated.
          I think that's too narrow. An exception can be handled even if you are
          not currently inside of a catch block, can't it?

          #include <exception>
          #include <stdexcept>
          #include <iostream>

          void handleException ()
          {
          try
          {
          throw;
          }
          catch (std::exception const &exc)
          {
          std::cout << exc.what() << "\n";
          }
          catch (...)
          {
          std::cout << "unknown exception\n";
          }
          }

          int main()
          {
          try
          {
          throw std::runtime_er ror("test");
          }
          catch (...)
          {
          handleException ();
          }
          }

          Isn't this a perfectly standard-conforming program? It does not call
          terminate() when compiled and run with VC or GCC.


          --
          Christian Hackl

          Comment

          • Pete Becker

            #6
            Re: try-catch usage

            On 2008-08-26 12:44:48 -0400, Christian Hackl <hacki@sbox.tug raz.atsaid:
            red floyd wrote:
            >
            >Per 15.1/8, "If no exception is presently being handled, executing a /
            >throw-expression/ with no operand calls terminate()."
            >>
            >In other words, a "throw;" must occur inside a catch block, or you
            >will be terminated.
            >
            I think that's too narrow. An exception can be handled even if you are
            not currently inside of a catch block, can't it?
            It depends on just what you mean by "inside" a catch block. Most people
            would say that the throw; below is inside a catch block. If you call
            handleException directly from main, i.e. not inside any catch block,
            the program will, indeed, be terminated.
            >
            #include <exception>
            #include <stdexcept>
            #include <iostream>
            >
            void handleException ()
            {
            try
            {
            throw;
            }
            catch (std::exception const &exc)
            {
            std::cout << exc.what() << "\n";
            }
            catch (...)
            {
            std::cout << "unknown exception\n";
            }
            }
            >
            int main()
            {
            try
            {
            throw std::runtime_er ror("test");
            }
            catch (...)
            {
            handleException ();
            }
            }
            >
            Isn't this a perfectly standard-conforming program? It does not call
            terminate() when compiled and run with VC or GCC.

            --
            Pete
            Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
            Standard C++ Library Extensions: a Tutorial and Reference
            (www.petebecker.com/tr1book)

            Comment

            Working...