Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

longjmp/setjmp in gfortran & error handling in general

Status
Not open for further replies.

sander314

Programmer
Dec 12, 2009
2
GB
I'm using a library in which all examples are in the style "single function with 1000 lines". Trying to understand and structure it a bit better by wrapping some stuff in functions, which can then be easily reused.
However, all the subroutines in the library take three extra arguments:
- error code
- error string
- alternate return label in case of error

Wrapping this up in a function means I have to have alternate return labels in every single one, and either have the functions in the same "program" section as the main example (so copy-pasting code), or also have the error code/string arguments for all my functions. This gets very messy.

I haven't used fortran 90 very much, and have more of a c++ background, but my idea to avoid this was:
- put the error message/code in a module, with setters/getters and use the module as some giant global error variable
- in the same module, have some kind of throw/catch exception handling, giving the exception label only once, and then jumping to it on an error call.

However, gfortran does not seem to have the longjmp or (i)setjmp calls.
Questions:
- Is there some way to use this in gfortran?
- Is this a good idea in general, what are alternatives for error handling?

My current code can be seen at:
 
I suppose the question is whether you are using gfortran as Fortran 90 or as Fortran 95. Fortran 90 has something called an assigned goto. You could have something like err.fcb which contains
Code:
   integer errhand, errval
   common /errfcb/ errhand, errval
And
Code:
program main
   include 'err.fcb'

   assign 999 to errhand

   ...

999 continue
   print *, 'Error ', errval
   stop
   end
...

subroutine xxx
   include 'err.fcb'
   ...
   if (errcond) goto errhand
end subroutine xxx
This will work in some versions of F90 but I haven't tried it for a very long time so I can't really say where it works and where it doesn't. Some compilers don't allow jumps between routines. Assigned gotos have been removed in F95. Somehow the standards people have the knack of removing the most useful features.
 
I'm using gfortran with fortran 90. Could probably use fortran 95 as well.

Tried it, gave some warnings:

Warning: Deleted feature: Assigned GOTO statement at (1)
Warning: Deleted feature: ASSIGN statement at (1)

But more importantly does not work between modules.
The module doesn't compile, since

Error: Variable 'errhand' has not been assigned a target label at (1)

And the file doesn't compile, since:
Fatal Error: Can't open module file 'errorhandling.mod' for reading at (1)

Also, this seems like a worse idea than longjmp, since an assigned goto like this won't actually change the stack (right?).
 
Oh well, I suppose that was wishful thinking.

You're right - assigned gotos don't change the stack. In fact there isn't anything in Fortran that unwinds the stack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top