Exception Handling

General help with the eC language.
Post Reply
fedor_bel
Posts: 21
Joined: Sun Mar 14, 2010 4:46 pm

Exception Handling

Post by fedor_bel »

Hello,

Does Ecere provide some exception handling mechanism?
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Exception Handling

Post by jerome »

Sadly (if exception handling is your thing), no :(

You can use the C mechanism of longjmp() ('throw') and setjmp() ('catch') if you really need it.

However I like to code without exception handling, by writing in a way some describe as the staircase effect... Here is an example of loading something where some things can go wrong:

Code: Select all

MyObject LoadSomeGoodness(String filename)
{
   MyObject result = null;
   File f = FileOpen(fileName, read);
   if(f)
   {
      char header[10];
      if(f.Read(header, 1, 10) && !memcmp(header, "GOODOBJv10", 10))
      {
         MyObject object { };
         if(f.Get(object.var1) && f.Get(object.var2))
            result = object;
         if(!result)
            delete object;
      }
      delete f;
   }
   return result;
}
So some of my own related eC coding guidelines:
-Try to have a single exit point
- Don't use goto
- Free things at only one spot, at the end of the block in which those things have been allocated. See how I delete the File and MyObject instance above. This way it will ensure that all (and only those) things that need to be freed get freed in case of failure.
- Make a function return either a boolean value: true for success, false for failure, or an object: null for failure, the object otherwise. For the latter case, see how I make use of a separate instance variable which gets assigned to result above, so that the object does not get to be allocated at the top of the function.


eC has destructors, so you can make use of that to keep the tidying up clean.

These nested if statements is pretty much my way of handling things going wrong. Some people hate it. I find it easy to follow, and with widescreens being popular these days we don't need to limit the code to 80 columns nowadays :)

I'm sorry if you were hoping I'd say 'yes it does, here's how'.

All the best,

Jerome
fedor_bel
Posts: 21
Joined: Sun Mar 14, 2010 4:46 pm

Re: Exception Handling

Post by fedor_bel »

Dear Jerome,

Don't worry. I actually did not really expect that you can bring up exception handling just out of the pocket. I am not a kid and see that in the world without automatic garbage collection and the stack structure and rules present there can be no such exception handling as in Java.
I am not a big fan of exceptions either. It took me years to understand why would one need them in Java and how to use them correctly :-)
I think one can go along pretty well with the rules you gave: returning either true/false or object/null and checking the result after each function call. I even like that coding style - that way you feel that you have considered almost every possible case and did a good thourough job.

Cheers,
Fedor
Post Reply