Re: [AD] 4.3 error handling

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


>>> (To note, I'm not saying this should move to C++. I'm just saying don't
>>> be surprised when people start trying out clever "tricks" to get added
>>> functionality. And when such "tricks" work, I don't really see a problem
>>> with using them)
>>>       
>> If people want to pull tricks in their own applications, fine, but
>> enshrining such tricks in an API is not a good idea.
>>     
>
> I wouldn't really consider the basic concept a trick though. The very first 
> line in the man page description for setjmp/longjmp is this:
>
> "setjmp() and longjmp() are useful for dealing with errors and interrupts 
> encountered in a low-level subroutine of a program"
>
> This is exactly what it's being used for.
>
>   
The whole thing just feels fishy to me, but I dont really have any
technical reason to not use this trickery.. anyway the last line in the
man page is

"setjmp() and sigsetjmp() make programs hard to  understand  and 
maintain. If possible an alternative should be used."

I guess that sort of sums up my feelings. try/catch isnt part of the C
language and although you can emulate it using setjmp/longjmp its not
obvious to many people how this works. After working with high level
languages for some time my opinion of C is its a very stupid language
and all the complex things should be taken care of at a different level.
Honestly I would never use straight C to write a game anymore; at least
C++ and then I can write my own error handling using standard language
features.

I do like try/catch, I think it can make code quite beautiful I just
question the added complexity of doing it in C. As an alternative what
about adding the return code to an accumulator and checking if that is
non-zero at the end of some block of code. If its non-zero, an error
occurred. This would only work if subsequent lines of code wouldnt crash
the program if the previous lines failed for whatever reason. As an
example( taking Chris's previous code ):

int errors = 0;
   audio_driver = al_audio_init_driver(NULL);

   main_voice = al_voice_create(audio_driver, 44100, AL_AUDIO_16_BIT_INT, 
AL_AUDIO_4_CH, 0); // return NULL if audio_driver is NULL
   errors += al_voice_get_long(main_voice, AL_AUDIO_FREQUENCY, &voice_freq);
   errors += al_voice_get_enum(main_voice, AL_AUDIO_CHANNELS, &channels);

   main_mixer = al_mixer_create(voice_freq, AL_AUDIO_32_BIT_FLOAT, channels);

   errors += al_voice_attach_mixer(main_voice, main_mixer);

if ( errors || ! audio_driver || ! main_mixer ){
   // .. error handling ...
}






Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/