-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Exception Handling #6
Comments
try: except: would be nice... right now, my best implementation at this is a universal base object with an error stack... mixed with preprocessor macros to pull out file/line numbers. This leaves a lot to be desired. |
A snippet example of your approach is appreciated. I have tried something similar, but I have never been satisfied... I like to see your approach. Cheers |
I think this proposal needs more clearly defined requirements. What would the exceptions look like? Would it be some kind of intrinsic exception type that could be defined and filled in by the client code and returned as argument from subroutines? Or perhaps something more Pythonic like try/except as Zaak suggested? Honestly I can't imagine how this would work in Fortran. AFAIK statements in Fortran either just work, or they crash the program alltogether. The best I can imagine is a standard-defined derived type that could be used by the user to set error code, message, level of severity, and raise method. @zbeekman Any chance for a sneak peek into your solution? The best I ever did was an integer return code from subroutines and testing its value upon subroutine return. |
Also keep in mind the |
Here are some thoughts on how it could be done which would be minimally invasive to the language. They would require both a new module and some new keywords. There would be a module with a base There would be the new keywords During the routine, a user can call try
call some_failable_subroutine()
except (a)
class is(io_exception)
print*, 'IO', a%get_message()
type is(os_exception)
print*, 'OS', a%get_message()
class default
print*, 'Some other sort of exception occurred'
end try if a failable procedure is called outside of a try
call some_failable_subroutine_called_outside_of_try_block()
except (a)
class default
throw a
end try If a Intrinsic procedures and operations may only produce exceptions of their own when called from the main program or a context with an explicit interface (needed so that the routine can be made implicitly Alternatively, rather than a procedure attribute, we could use something more like the Unfortunately, none of this is particularly compatible with how This is all just thinking off the top of my head, so there may be obvious problems or things which aren't clear. I just figured I'd put it out there. Thoughts? |
@milancurcic heh, it's not pretty... I'll try to find the code, or reproduce the underlying principle skeletally from memory. This is the gist of it: You maintain a call stack explicitly. Define an "error stack" base object, with the following data components:
Now every time you call a procedure the procedure requires an additional dummy argument which can be expanded by macros with most preprocessors/compilers. Something like: call my_sub(arg1, arg2 ... , file_line=__FILE__//':'//__LINE__) The The first thing any procedure does upon entering is push it's name and file/line onto the call stack. If it encounters an error it can call The exception object can either be explicitly declared and passed around, or it can be the passed argument of any method that extends the error type. One must be careful though, since you typically want a singleton (or one error stack per image/MPI rank/thread etc.)... At any rate it's not the most elegant solution. I can't find my original code, and am working on posting something new, time permitting. The one benefit is that it can be used in |
Hi @ALL, Sorry I'm not following too much this threads ... 😓 I've a dummy-project to play with an implementation of "exceptions" in Fortran https://github.com/victorsndvg/ForEx . From my point of view, the bigger problem here is that Fortran does not allow non-local jumps (http://publications.gbdirect.co.uk/c_book/chapter9/nonlocal_jumps.html). Without non-local jumps, you can control exceptions on "vertical subroutine hierarchies", but not if there are several try-catch blocks at the same level, you are not able to control the flow while catching exceptions. In addition to what @cmacmackin said, I think a What do you think? |
No description provided.
The text was updated successfully, but these errors were encountered: