CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2007
    Posts
    609

    How to catch exeption if I don't know what it is?

    How can I write code that will catch an exeption, if I don't know what to expect?

    Ex, in C# I can do this:

    Code:
    try
    {
    //lot of code
    }
    			catch (Exception ex)
                {
    				Console.Write("error: {0}",ex.ToString();
                }
    Is there a way to do this in C++? Often, it's not known what type of exception can be thrown, so it's hard to try to catch specific ones, and often times some can be missed, especially when using 3rd party libraries.
    http://www.uovalor.com :: Free UO Server

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to catch exeption if I don't know what it is?

    Well, you could write:
    Code:
    try
    {
        // your code
    }
    catch (const std::exception& e)
    {
        // handle the exception
    }
    catch (...)
    {
        // handle the exception
    }
    Unfortunately, this does not allow you to actually obtain any information from those non-std::exception based exceptions: you only know that you have caught such an exception.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to catch exeption if I don't know what it is?

    Quote Originally Posted by Red Squirrel View Post
    Often, it's not known what type of exception can be thrown, so it's hard to try to catch specific ones,
    If you don't know if an exception is thrown and what that exception may be, then that is the fault of the documentation for the particular function(s) that you're calling.

    This is where proper documentation is a must -- no function that explicitly throws exceptions should keep it a secret from the persons using those functions.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 3rd, 2011 at 03:51 PM.

  4. #4
    Join Date
    Jul 2007
    Posts
    609

    Re: How to catch exeption if I don't know what it is?

    Quote Originally Posted by laserlight View Post
    Well, you could write:
    Code:
    try
    {
        // your code
    }
    catch (const std::exception& e)
    {
        // handle the exception
    }
    catch (...)
    {
        // handle the exception
    }
    Unfortunately, this does not allow you to actually obtain any information from those non-std::exception based exceptions: you only know that you have caught such an exception.
    Hmm so there's no way to actually spit out exact info like in C#?

    What if a function can return 100 different types of exceptions, it just seems crazy that I'd have to handle each one individually. I must be overlooking something. I've never really played with exceptions much but I'm running into situations where I need to and it's probably a good concept to learn anyway.
    http://www.uovalor.com :: Free UO Server

  5. #5
    Join Date
    Jul 2007
    Posts
    609

    Re: How to catch exeption if I don't know what it is?

    Quote Originally Posted by Paul McKenzie View Post
    If you don't know if an exception is thrown and what that exception may be, then that is the fault of the documentation for the particular function(s) that you're calling.

    This is where proper documentation is a must -- no function that explicitly throws exceptions should keep it a secrret from the persons using those functions.

    Regards,

    Paul McKenzie
    Yeah that's a good point, I guess in 99% of situations I can just google to see what type of exception it can throw.
    http://www.uovalor.com :: Free UO Server

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to catch exeption if I don't know what it is?

    Quote Originally Posted by Red Squirrel View Post
    What if a function can return 100 different types of exceptions
    It is highly unlikely that the caller would need to be responsible for handling all 100 failure cases. Choose of a subset of the cases that you know how to handle and deal with them; allow the rest to pass, possibly to be caught at some higher level.

    In any event I would think such a function would be poorly designed in the first place.

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: How to catch exeption if I don't know what it is?

    Quote Originally Posted by Red Squirrel View Post
    Hmm so there's no way to actually spit out exact info like in C#?

    What if a function can return 100 different types of exceptions, it just seems crazy that I'd have to handle each one individually. I must be overlooking something. I've never really played with exceptions much but I'm running into situations where I need to and it's probably a good concept to learn anyway.
    C# is a managed and weakly typed language. You can determine a lot about the object at runtime. This makes it more flexible than C++, but also slower and bulkier.

    You can use the typeof() feature of g++. But again, it's handled at compile time.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to catch exeption if I don't know what it is?

    Quote Originally Posted by Red Squirrel
    Hmm so there's no way to actually spit out exact info like in C#?
    Maybe there is: it depends on what you mean by "exact info", and what exactly is available for you to obtain from the exception. My comment about non-std::exception-based exceptions applies in the rather extreme case where you have no clue what else could be thrown, hence the use of the catch-all. Typically, exceptions are derived (perhaps indirectly) from std::exception, or at least from some other exception base class, thus you can access the what() member function or its equivalent.

    Quote Originally Posted by Red Squirrel
    What if a function can return 100 different types of exceptions, it just seems crazy that I'd have to handle each one individually.
    How did you do handle such a situation in C#? If you really have to handle each one individually, you just have to do it, whether in C++ or C#, but as Lindley pointed out, you probably don't.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured