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?
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?
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.
let say I'm going to read a file that was not there, how can I stop program from giving me error ?
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
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.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 ); }
> 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.
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.
Do you even know what you're talking about? Off topic, and "mac os ipod" is not something you write programs for.
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.
So exactly what part of "check your return values" did you not understand?
Quzah.
Hope is the first step on the road to disappointment.
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.Everytime when I hit a file error or a wrong scanf my program will show me a pop up and end the program.