Thread: How do you deal with run-time errors?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41

    How do you deal with run-time errors?

    I was just curious what the recommended practice is when it comes to dealing with run-time errors. I have seen assert() used but I thought I read that it is not good practice to use assertions because they might not work with some compilers. Is is better to us the try/catch method? Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by waterborne
    I was just curious what the recommended practice is when it comes to dealing with run-time errors.
    You might want to be more specific when talking about run-time errors, e.g., provide some examples of what you are talking about.

    Quote Originally Posted by waterborne
    I have seen assert() used but I thought I read that it is not good practice to use assertions because they might not work with some compilers.
    I have never heard of such reasoning. Assertions would be inappropriate if you actually need to handle the error rather than just detect it. In a program optimised for release, assertions may be disabled, so they should be considered a (proactive) debugging tool.

    Quote Originally Posted by waterborne
    Is is better to us the try/catch method?
    It depends. For example, you might use an assertion to check for a pre-condition violation in a function that is just implementation detail, but you might throw an exception for a pre-condition violation in a function that is part of the interface.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    I guess I meant along the lines of opening a file and checking to make sure it actually opened. Things of that nature. It seems like there are a lot of different ways to do that but I have a feeling there is a "preferred" method. Does that make sense?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In such a case, throwing an exception or returning an error code would be more appropriate than an assertion, since this is not a logic error, but a routine run-time error. In fact, because it is arguably an "expected error", some would shun throwing an exception as handling it could be considered part of the normal execution of the program. But this is debatable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Asserts are good to detect bugs or things "that should never be" in your code. They as a means of debugging whereas exceptions are a means of controlling the logic in your program.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    Colorado
    Posts
    41
    Thanks for the clarification - that makes a lot of sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NtSetSystemTime() failed
    By medp7060 in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2010, 02:58 AM
  2. Average asymptotic run time?
    By Ariod in forum C Programming
    Replies: 1
    Last Post: 08-03-2005, 06:47 PM
  3. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 02:58 AM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  5. Create class at run time
    By muaed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-11-2002, 08:13 AM