CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Try / Catch

  1. #1
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Try / Catch

    Can someone point me to a web page or a book that contains a good explanation of using the try and catch statements in C++.

    I think I basically understand their use, but I have never really used them before. I want to make sure I have a good understanding of them before I really begin to use them in production software.

    Thanks for any help.

    Kendall
    John 3:16
    For God so loved the world ...

  2. #2
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: Try / Catch

    Hi,

    this link may be useful to you...

    http://msdn.microsoft.com/en-us/library/4t3saedz.aspx

    -Anant
    "Devise the simplest possible solution that solves the problems"

  3. #3
    Join Date
    Jan 2008
    Location
    India
    Posts
    408

    Thumbs up Re: Try / Catch

    MSDN! Its always nice

    http://msdn.microsoft.com/hi-in/libr...us,VS.80).aspx

    Happy learning!

    Bharani.
    Rate the posts which you find useful

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Try / Catch

    One thing the MSDN intro seems to neglect is that ideally all exceptions should be derived from std::exception.
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    May 2003
    Location
    San Antonio TX
    Posts
    380

    Re: Try / Catch

    Those links have proven to be very helpful. I appreciate the information.

    However, I am also looking for information on the hows and whys of using try catch as opposed to returning error codes.
    John 3:16
    For God so loved the world ...

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Try / Catch

    1) Know what will throw exceptions and when.
    2) Throw your own exceptions by value (a temporary), and catch as a reference /*to const if possible*/.
    3) Do not fill your code with catch(...). Use them rarely.
    4) Do not catch exceptions that you cannot handle.
    5) Catch exceptions that you can partially handle and then re-throw them.
    6) Exception specifications are good but I don't see them much in practice in C++ apart from empty throw() saying that a function doesn't throw.
    7) DO not over-use exceptions. Be comfortable distinguishing an exception and a logical program error.
    8) Derive your own exception classes from std::exception class. But don't make too many of them.
    9) Do not throw any type of exception esp. those not derived from std::exception and the fundamental types, for example, int, const char* etc.
    10) Make your code exception safe - this is not really related to exception handling but you should write code that doesn't fall apart in case an exception is throw. Search the web for David Abraham's writings on exception safety.

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