An experiment with exceptions

During the last monthly call I was reminded of the fact that Fortran does not support exceptions. While I cannot judge the complexities that are involved in implementing such a feature, I thought I could do some experimenting with the typical coding patterns you see in other languages. This has resulted in a repository on Github with a simple example. I would call it a form of cooperative exception handling, as it simply transforms try-catch-throw “statements” into regular Fortran code, but that relies on all the code being preprocessed. (That is probably avoiding all the difficulties a true exception handling feature must face.)

So a very limited and perhaps laughable attempt, but it might inspire someone - and it was fun to set up.

4 Likes

See this issue and the linked discussions:

Your approach seems similar to my comment here:

I’ll copy the relevant part here:

Essentially your macro system transforms it into the a.f90 style.

1 Like

The try block up to the first catch statement is enclosed in a do-loop, so that we can jump out without a goto statement

I think you can use exit to jump out of a block construct, so a do-loop shouldn’t be necessary.

try : block
    call subx( ... )
    if ( exception_raised ) exit try
    call suby( ... )
    if ( exception_raised ) exit try
        write(*,*) 'Answer: ',x, y
    exit try
end block try
3 Likes

Also, see this: Exceptions and Exception Handling · Issue #172 · j3-fortran/fortran_proposals · GitHub

So pre-processing is also not necessary.

1 Like

Nice! I realised that I would need to use a labelled do-loop to jump out of the right loop in case of nesting. But using a block construct is much more elegant.

I was writing up something on how the deprecated but useful alternate return has allowed for some of this for a long time, but then saw the link to just such a discussion in more length than I had written. I like the style the preprocessor lets you produce. Nice.

1 Like

Things are starting to fall in place with these discussions! The past discussions are archived and we are building upon them by referencing them. Finally we are not starting from scratch with every new idea. I am personally very happy about that, that is exactly what I was hoping to achieve by starting the “incubator repository” (GitHub - j3-fortran/fortran_proposals: Proposals for the Fortran Standard Committee).

My first attempt at exception handling used precisely the alternative return. I wrote about it in the Fortran Forum a few years back.

Thanks. I will take a look.

PS:

I have repeatedly heard the IBM scientific library has great error handling
IBM Engineering and Scientific Subroutine Library for Linux on POWER and OpenPOWER Version 6.3: ESSL Guide and Reference Version 6.3
but unfortunately do not have access to it,

Thanks for that link - I have downloaded the PDF for further study. I do not have access to that library either, but it may give some ideas!

I have found this:

I am not sure how accessible is this link by Springer.

Thanks, I could access it without a problem. I vaguely remember seeing it before, but that could be a false memory. Anyway, I got it now!