Wednesday, August 8, 2007

No exception handling method in C? Hah!!!

quite a few times, i get noob programmers declaring that C is just plain hard, it sucks and stuff like that. well, i pity them for not njoying the best programming language ever made. even more laughable is when they say that C is buggy, coz it doesnt have exception handling. now i have my sides splitting. whoever gave them that slanderous thought. to start with, core C library functions, almost every one of them, at least each and every ANSI C and POSIX.1 extensions have exception handling. u dont see them as the try..catch blocks that u typically expect to see in say C++ or Java, u see simpler things like return values. negative returns, null values returned by functions MUST be used to check if it is okay to proceed further in the program. this wonderful concept was completely destroyed by the advent of void functions. as long as C stuck to ANSI standards, a minimum of int return value was mandatory, but the acceptance of void return type by the POSIX standard completely annihilated the concept of return value checking if it is safe to proceed further. instead they now use some kind of crappy referee kinda thing called exception class, which is created and raised every time the program reaches an unsafe state. languages now use try..catch and then keep holding their hands out in the air to catch possible faults and deal with them. this hypocrisy makes it look like the language really cares for programmers, but mates, what abt the overhead to create an exception, raise it, pass it onto a catch block in case faults occur. and what do u do if catch block catches u cold with some buggy code itself. and also it makes it harder to isolate the line which caused the bug. in C, u just execute a command and check return. if return is an error, u kill things right then and there, that line is the bug, job done. but in a catch block, in order to know the line that caused the error, u have to do fancy things like stack tracing causing more overhead. u think subclassing exceptions is cool, so that u get different categories, rite? oh bull shit, in C u dont need objects and sub-objects / hierarchy, nothing. just make the function return different values for different errors, make an enum that makes it convenient to call those error numbers by friendly names and just go ahead compare the return value with the values in the enum to see what went wrong. in fact, that enum is not even needed, just that it makes code pretty readable and modular. so friends, steer clear of exceptions and consider return values seriously.

No comments: