Thread: ignoring exception - is this a problem?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    34

    ignoring exception - is this a problem?

    If the function func() throws an exception, but we don't call the func() from try clause (to at least swallow the exception), let alone use catch clause, would that possibly show some undefined behaviors later on?

    Code:
    int main()
    {
    ...condition is setup for func() to throw an exception...
        func();
    ...program goes on...
        return 0;
    }
    I was reading a c++ reference book, and it was talking about a function storing exception information into a call stack after each of a try or catch clause. Did I get that right? Then from the function below if func2() throws an exception, does that get stored in func2's call stack, or func1's call stack.

    So in general, I'm confused as to how and why call stack is used to keep the exception information and what consequences I have related to this if I don't swallow/catch the exception like the example shown above.

    Code:
    int func1()
    {
    ...
    try{
        func2();
    }
    ...
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chiefmonkey
    So in general, I'm confused as to how and why call stack is used to keep the exception information and what consequences I have related to this if I don't swallow/catch the exception like the example shown above.
    I cannot answer your question about an implementation of exceptions since I have never investigated the topic, but I can tell you that in general the lack of a try/catch construct is not a problem since the exception will just propagate. However, you should not allow exceptions to propagate across module boundaries. In your example, this means that you should have a try/catch for the main function if func() might throw or propagate an exception.
    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 VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Ignoring an exception is valid from a compile standpoint but not from a code standpoint. Exceptions are usually not thrown unless critical errors have occurred. So what you are asking is if it is ok to ignore a critical error. My answer would be no. If it is not a critical error then an exception should not be thrown and the error could be dealt with via return values or some other mechanism.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If you were forced to write a try block, you'd need a catch block, since the two are always paired. But how could you possibly know what to place inside the catch block if you did not know the semantics of the exception? Code can call other code. This code might throw an exception. You are not forced to handle this exception exactly because you might not know how to handle it.

    So no, it's perfectly valid to not catch every exception at every point in the code (in fact, it sounds a bit strange when put that way doesn't it?)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Normally when a third party library function or DLL function can throw an exception it will be documented in the readme or the docs that come with it. And you can always catch the exception even if you do not know what it is using a catch all. It does not give you much information but it does allow you to notify the user of the app that something bad has happened and it allows you some way to gracefully shut down or handle the issue.

    Catching exceptions is a simple way to diagnose and correct errors even though once you fix the issue at hand the exception may never be caught again. I know of software companies that require you to catch every exception and check every single return value. A bit annoying and overkill but it does have some worth to it. If you get into the habit of ignoring exceptions you will eventually ignore one that will cause major problems.

    There is also code you can write that will give you a stack trace even in an exception in release mode. This is talked about in MS's reference about debugging inside Visual Studio. The 2003 debugger did not provide a stack trace but it was possible to get one with some assembly and C code. I'm not sure if 2005 and 2008 have more functionality in the GUI-based debugger but I seriously doubt it. The core debugger is extremely powerful but the GUI-based front-end to it that ships with MSVS is a bit gimped and does not use all the features.
    Last edited by VirtualAce; 08-21-2009 at 09:00 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bubba
    Catching exceptions is a simple way to diagnose and correct errors even though once you fix the issue at hand the exception may never be caught again. I know of software companies that require you to catch every exception and check every single return value. A bit annoying and overkill but it does have some worth to it. If you get into the habit of ignoring exceptions you will eventually ignore one that will cause major problems.
    I agree that one should catch every exception and check every single return value. However, catching every exception is not the same as placing a try/catch around every block of code from which an exception might be thrown. Unless it means propagation across a module boundary, an exception should be allowed to propagate if it cannot be handled or translated appropriately at a given point.
    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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    I agree that one should catch every exception and check every single return value. However, catching every exception is not the same as placing a try/catch around every block of code from which an exception might be thrown. Unless it means propagation across a module boundary, an exception should be allowed to propagate if it cannot be handled or translated appropriately at a given point.
    Hear, hear!

    Among the many gripes in my life are the programmers who insist that "must catch every exception" equates to "every function must have a try/catch block in it to catch every exception".

    One of the design rationales of exceptions is that they need not be caught by code that can't do something rational to correct the triggering error condition.

    Unfortunately, crossing module boundaries (eg an exception propagated from a DLL to the calling application) often causes problems for various reasons.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    However, catching every exception is not the same as placing a try/catch around every block of code from which an exception might be thrown. Unless it means propagation across a module boundary, an exception should be allowed to propagate if it cannot be handled or translated appropriately at a given point.
    Agreed and well stated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  2. stack trace in exception
    By George2 in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2008, 03:00 AM
  3. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM
  4. Replies: 2
    Last Post: 06-21-2006, 04:23 AM
  5. problem with exception behavior
    By agarwaga in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2006, 05:32 PM