how do I catch exception in c?

This is a discussion on how do I catch exception in c? within the C Programming forums, part of the General Programming Boards category; Hi everyone, I would like to check with you all, how can I catch all the exception in c?? eg. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    58

    how do I catch exception in c?

    Hi everyone,

    I would like to check with you all, how can I catch all the exception in c?? eg. like scanf or fopen, how can I stop the program from crushing if no file was to be found, or even when the user tried to enter a different input?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Know the return values of the functions you use, what they mean, and check for them. If you're looking for a try...catch mechanism, you won't find that in standard C. C++ implemented it later.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    let say I'm going to read a file that was not there, how can I stop program from giving me error ?

  4. #4
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Windows supports SEH (Structured Exception Handling), which is just like C++ exception handling, other than the fact that it states that it is not a good idea to use it with C++ (it is designed for C).

    so if you are using MSVC++, take a look at this link:

    http://msdn.microsoft.com/library/de.../key_s-z_4.asp

    or you can use if else statements to check for certain function return values like citizen said, whatever tickles your pickle.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Code:
    /* 1. Make an attempt. */
    FILE *fblah = fopen( "blah.txt", "r" );
    /* 2. fopen returns NULL if it exploded */
    if ( fblah == NULL )
    {
       /* 3. What happened? */
       perror( "fopen error" );
       /* 4. Handle it somehow: open another file instead, or close. */
       exit( EXIT_FAILURE );  
    }
    It is a big misconception to assume that all errors should, or could, be handled quietly. Often it is better to understand what your program actually does and handle errors that are in the problem domain that your work attempts to solve. Technical problems are harder to deal with. Sometimes its better to assert something and die.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > I would like to check with you all, how can I catch all the exception in c?
    Standard C doesn't have exceptions.

    > eg. like scanf or fopen,
    You look at the return result, then decide what to do about it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    You can do a few trick ways or the correct way. The cheating way is to add a scanf at the end of your program so the instructions have you true program finished and waits on your input to end the program. The correct thing to do is add a return and then run in windows on the dos prompt run it. CD mydoc/so on and so forth and run. That way when the program is finished it will just start a new line in the prompt. Without using trying OS I would assume the same will work in Linux (ubuntu!) and Mac OS ipod.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Do you even know what you're talking about? Off topic, and "mac os ipod" is not something you write programs for.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    58
    I understand what all of you are suggesting. let say if I open file and I would like to close it and continue with the rest of the program. Will it be possible ? Everytime when I hit a file error or a wrong scanf my program will show me a pop up and end the program. I could only print out the error msg, but what I want is to print out the error msg, and continue with the rest of the program.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So exactly what part of "check your return values" did you not understand?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Everytime when I hit a file error or a wrong scanf my program will show me a pop up and end the program.
    Sounds more like every time you try to read from an invalid file descriptor you get an error. fopen() won't cause any exceptions. You need to validate if the return value from fopen() is ok or not before you start reading or writing to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 06:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 04:28 PM
  3. How do I catch an exception thrown from a linked DLL?
    By 6tr6tr in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 07:49 AM
  4. unexpected exception handler
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2008, 04:49 AM
  5. exception try, catch
    By arjunajay in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2006, 07:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21