tags:

views:

129

answers:

3

I'm looking to implement exceptions with nestable try-catch-finally statement with messages in C using longjmp/setjmp.

I've managed to implement try-catch-else exceptions, they are not nestable. I'm also hoping to add messages to the exceptions. Any idea how I might be able to do it?

A: 

Well, you can't really implement exceptions in C, since they're not supported by the language. The best you can do is emulate them using setjmp and longjmp and some diabolically clever macros.

A quick search turns up these links that may be useful to you:

Daniel Pryden
I'm looking for advance implementation, and yes you can actually implement them in C. Just that the syntax is not straightforward. As for nesting and messages, they are possible as well. Your answer is not really an answer, by assuming I haven't googled before posting this, let alone to having a few days of though prior to this, is just sheer reputation whoring. -1
nubela
of thought* (15 chars restriction)
nubela
@Daniel You can implement everything in C, just because C is sort of portable assembler so everything that can run on the computer can be implemented using C. C lacks syntactic sugar for many things, like OOP and exception, but there are ways to get around it. Remember - first C++ compiler (Cfront) was producing plain C code.
qrdl
@nubela: See http://www.catb.org/~esr/faqs/smart-questions.html. If you've done background research, tell us what you've found and why that isn't the answer.
Jonathan Leffler
@qrdl: cfront had to be abandoned when exceptions were added to C++ because C could not deal with C++ exception handling.
Jonathan Leffler
@nubela: It is not clear that exceptions can be implemented in C - at least, not exceptions in the style of C++. You can create various approximations - another can be found in Hanson's "C Interfaces and Implementations" at http://sites.google.com/site/cinterfacesimplementations/ - but they all have imperfections compared with exceptions implemented by the compiler.
Jonathan Leffler
@Jonathan Leffler not disagreeing, but do you know that it was actually impossible to do C++ exceptions using cfront, or just inconveniently complex? Any reference material so we could understand *why*?
djna
@djna: I'd have to go digging - two places I'd be looking are 'Design and Evolution of C++' by Stroustrup and 'Inside the C++ Object Model' by Lippman.
Jonathan Leffler
@nubela: Wow, harsh! The phrasing of your question gave no indication of what level of research you had done, so I figured to start with the basics. I stand by my first statement, which is basically what Jonathan Leffler appears to be saying here, which is that the only way to *truly* implement exceptions in C is to essentially build a new language on top of C (cf. Greenspun's Tenth Rule). At the very least, to nest exceptions and attach messages, you'll need to implement your own stack.
Daniel Pryden
+3  A: 

For nesting: a stack-frame of current try/catch blocks.

Your try will be using setjmp to save to a jmpbuffer (I guess). If you've done a try, and hence are now in the scope of a try block and hit another try then you want to preserve the existing jmpbuffer and also create a new one - Push - and when catching you are longjmp-ing back to the point of the most recent try hence you Pop the latest jmpbuffer. So I think a stack-like model make sense for nested try/catch.

For implementation, I guess the simplest apporach is to reserve an array of jmpbuffers, hence limiting your try catch depth - but keeping it simple; Push and Pop just require you to track the index in that array.

For messages and other exception contents, a reserved area for "currentException".

Exception content. Keep it simple, define an Exception struct. A char array and an int. Keeping it simple, but not too simple, reserve an array of them so that you can support chaining.

For a throw you allow

 throw  ( "string", errcode )

Which simply zeros the array structure and makes one entry. And

 catch ( exception )

Now can look in the array and finds the first entry, and then

 throwChain ( "string", errcode)

Which adds the new exception to the array (if there is room, and if not can shuffle the array according some rule such as FIFO)

But, I've got to ask, why not just use C++?

djna
Interesting, care to elaborate?I'm actually exploring to implement a toy compiler in C. So yeap.
nubela
I'll add a little more, but I am just making this up on the spur of the moment :-(
djna
Please do ty :)
nubela
+2  A: 
Norman Ramsey