Hi
Is there a possiblity of handling exceptions in C as in Java and C++, Of course Error handling not based on the retuntype of the function, Exception should handle runtime error like divide by zero etc.

Is that possible????:confused:

09-03-2001

Sebastiani

C is really more a build- it- yourself language.

Exception handling really means what you decide to do when errors occur.
For instance, let's say a user inputs a zero to calculate. Now if you didn't want the program to abort you could check for equivelance with zero first, and if so print a "bad val" message and then re-prompt the user for input. You could even print and save in an error log which lists the timeof day, error code etc. This is an example of do-it-yourself error handling.
So no I don't think C has any built-in error handling.
oh What is wrong with using the return value of a function? How hard is
if(bad_val){ ...?
if(!devide(your_num)){ ...?

I take your point but what I was trying to do was avoid a situation where I have to test for all sorts of possible conditions, thereby slowing my ( time critical ) program down. I thought ( naively ) that _try/_except would solve the time problem. Actually it does the job but is no faster than before. Interesting though ...

I guess experience is just that - you keep trying things. Some work - some don't. Must say that I can see why some people hate C and others love it. I am joining the ranks of the devotees because it allows you to get right up to the machinery ( especially if you are an asm fan as well ). To the non devotees it must be a pain in the rear ( "what's wrong with Basic and who the hell needs pointers anyway?" )

I guess the test is whether you prefer to drive an automatic car or a manual gear change ( "stick shift" if you speak USA )

01-26-2002

SilentStrike

Exceptions are no faster when they aren't thrown, and slower when they are as opposed to the standard error checking by the value returned.

Where they are beneficial is in client code in which can handle multiple error conditions in the same block, in forcing a client to handle an error, rather than let the program continue onto the real of interminate (and often erratic) behavior because an error was not handled, and in the few cases were all possible outputs for a function are valid, but not all inputs are (thing about natural log, in which the output is all reals, but the input must be greater than 0).

01-27-2002

Shiro

>I need to carry on if my prog divides by zero and this works

Strange. If you'r program divides by zero, then from a mathematical point of view, j would have an undefined value. And so the behaviour of your program would become unpredictable.