is there a try-catch statement in C? or an external library someone made? would be 开发者_JAVA技巧very useful
if not, is there a way one can determine if a variable is an array?
is there a try-catch statement in C? or an external library someone made? would be very useful
On Windows there is SEH, but you really shouldn't use that for general application usage. Without something like C++ destructors it's impossible to write truly exception safe code.
if not, is there a way one can determine if a variable is an array?
Yes. You keep track of it yourself.
There is no try-catch statement in C, but you can build an exception mechanism using jump buffers. However, that's probably a very bad idea as there is now way to automatically release resources when an exception is thrown.
If you are referring to a void *
as a variable, then there is no way to determine if it is an array. However, you can build logic into your application to achieve runtime type information (RTTI) as well.
I'm developing exceptions4c, an exception handling system in C (portable ANSI C) that currently supports: throw
, try
, catch
, finally
and a few more goodies. For example, it supports the Dispose pattern, so you can automatically release resources. You can also handle signals (such as SIGFPE
and SIGSEGV
) as if they were exceptions.
There is no native support for exceptions in C, of course.
Take a look at the following link, basically, it makes use of setjmp() and longjmp():
http://www.on-time.com/ddj0011.htm
try-catch is a scheme for error handing in an object oriented environment like C++. ANSI C is not object oriented and it requires you declare your variable types. In C, error handling is performed primarily by checking returned values of called functions. You can see more in the messy thread here: ANSI C equivalent of try/catch?
If the compiler can determine if a variable is an array, so can you. Just look where it's declared (or malloc'd, in the case of a dynamically allocated array).
精彩评论