Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement exceptions with nestable try-catch-finally statement with messages in C

Tags:

c

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?

like image 228
nubela Avatar asked Nov 22 '09 16:11

nubela


People also ask

Can you catch exceptions in C?

C itself doesn't support exceptions but you can simulate them to a degree with setjmp and longjmp calls.

Can we use try catch and throws together?

Q #2) Can we use throws, try and catch in a single method? Answer: No. You cannot throw the exception and also catch it in the same method. The exception that is declared using throws is to be handled in the calling method that calls the method that has thrown the exception.

Can we write try catch inside finally block?

No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.

What should be put in a try block 1 statements that might cause exceptions 2 statements that should be skipped in case of an exception?

What should be put in a try block? Explanation: The statements which may cause problems are put in try block. Also, the statements which should not be executed after a problem accursed, are put in try block. Note that once an exception is caught, the control goes to the next line after the catch block.


2 Answers

Dave Hanson has already done a really nice package of exception macros as part of his excellent book C Interfaces and Implementations. You could either use the code wholesale or learn from his techniques. For anyone who does a fair amount of C programming, the book is worth buying—it will change the way you change about C programming, and it will show you how to do object-oriented design in C.

like image 142
Norman Ramsey Avatar answered Oct 24 '22 18:10

Norman Ramsey


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++?

like image 35
djna Avatar answered Oct 24 '22 18:10

djna