Thread: Error handling...

  1. #1
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718

    Error handling...

    I don't use C much, but I thought I'd mess around in it. Is try/catch C++ only? This code spits out errors with MSVC 6...
    Code:
    int main( void ) {
    	try {
    	}
    	catch( ... ) {
    	}
    	return 0;
    }
    Errors:
    Code:
    g:\c++\ctrycatch\main.c(2) : error C2065: 'try' : undeclared identifier
    g:\c++\ctrycatch\main.c(2) : error C2143: syntax error : missing ';' before '{'
    g:\c++\ctrycatch\main.c(4) : warning C4013: 'catch' undefined; assuming extern returning int
    g:\c++\ctrycatch\main.c(4) : error C2143: syntax error : missing ')' before '...'
    g:\c++\ctrycatch\main.c(4) : error C2059: syntax error : ')'
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well you cannot use try catch and throw. Those are C++ specific.

    ** M$VC defines some M$ specific functions you can use.

    They are preceeded by two underscores. Lookup __try __except __finally

    There may be some sort of equivalent that I am not aware of in C but I don't think so. Some people have written libraries that act like the C++ versions; you may want to look into that.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>Well you cannot use try catch and throw. Those are C++ specific.
    Thank you. That's what I wanted to know.

    Is there any standard way to do exception handling in C?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Lookup setjmp and longjmp. I generally use these for error handling in C. If you need some examples let me know.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by XSquared
    >>Well you cannot use try catch and throw. Those are C++ specific.
    Thank you. That's what I wanted to know.

    Is there any standard way to do exception handling in C?
    Sure. After returning from a function, check its return value. If an error, do somthing to clean up.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Here's a project involving exception handling in C.
    http://sourceforge.net/projects/cexcept/

    Personally I prefer the way WaltP describes. Developing robust code means that you perform error checking in your code en error handling.

Popular pages Recent additions subscribe to a feed