Thread: Question about Try/Catch

  1. #1
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60

    Question about Try/Catch

    Rewriting my firewall parser, and decided to start with solid error checking from the start using the try/catch methodology.

    My question is if I call a class function instead of using return on failure is it proper to just use throw?

    Like

    Code:
    int parsThis::myJunk(int infostuff) {
        int payoff;
        if (infostuff > 0) {
            //Do some crap change up payoff
            return payoff;
        }
        throw 1; // throw here instead of using return with a catch(int i) in main func
    }
    I have already done it, and it seems to work fine. It just doesn't look right to me, and wanted to make sure I'm not committing some programming crime against humanity ( yah goto im looking at you ).
    "The people don't want war, but they can always be brought to the bidding of the leaders. This is easy. All you have to do is tell them they are being attacked, and denounce the pacifists for lack of patriotism and for exposing the country to danger. It works the same in every country." - Hermann Goering.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If it is an exceptional circumsance then yes the best thing is to just throw, but generally one would not throw an int.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xentaka
    It just doesn't look right to me, and wanted to make sure I'm not committing some programming crime against humanity
    The crime that you are guilty of is throwing an integer as an exception. Rather, throw an exception derived from std::exception, e.g., std::invalid_argument from <stdexcept>. I might write it as:
    Code:
    int parsThis::myJunk(int infostuff) {
        if (infostuff <= 0) {
            throw std::invalid_argument("infostuff must be positive");
        }
    
        int payoff;
        //Do some crap change up payoff
        return payoff;
    }
    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

  4. #4
    Registered User xentaka's Avatar
    Join Date
    May 2011
    Posts
    60
    Yes! That is exactly what I needed.

    So I would do :

    Code:
    int parsThis::myJunk(int infostuff) {
        if (infostuff <= 0) {
            throw std::invalid_argument("infostuff must be positive");
        }
    
        int payoff;
        //Do some crap change up payoff
        return payoff;
    }
    
    int main() {
        try {
            //bunch of main stuff
        }
        catch(exception& e) {
            cout << "ERROR: " << e.what() << endl;
            return 1;
        } 
    }
    Thanks guys.
    Last edited by xentaka; 05-24-2011 at 09:00 AM.
    "The people don't want war, but they can always be brought to the bidding of the leaders. This is easy. All you have to do is tell them they are being attacked, and denounce the pacifists for lack of patriotism and for exposing the country to danger. It works the same in every country." - Hermann Goering.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Throw catch question
    By TriKri in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2009, 03:32 AM
  2. try & catch
    By kawafis44 in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2008, 04:04 PM
  3. Is there a catch to return in a try/catch box?
    By meili100 in forum C++ Programming
    Replies: 25
    Last Post: 11-21-2007, 01:33 PM
  4. try/catch
    By ssjnamek in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2005, 12:44 AM
  5. try ... catch
    By monkeymon in forum Windows Programming
    Replies: 5
    Last Post: 05-24-2002, 03:06 AM