- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
a dll written in fortran is called from c# but sometimes the dll go to an error and the execution of the program is cancelled. I would like to catch this as an exception in c# (try catch doesn't worked), it is somehow possible to do that? I'm also free to modifiy the fortran code...thank you!
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Steve, thank you very much for your answer.
Also, the dll that I use it's to calculate something. If that something it's notphysical possible, the dll show on the console an error and a STOP command is executed. Well, when the STOP command in the fortran code is executed, my code in C# is aborted as well. I would like to change that STOP to something else that abort the fortran code, tells to C#, something it's going wrong and if it's possible, parse the error message to the C# as well. Any Ideas?
You said it's not possible to catch I/O errors, array bounds violations, etc at the present. Will be possible on the future? Are you working on it or it's only an idea?
Thank you again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
STOP cannot be caught - it exits the program. You can perhsps see if RAISEQQ does what you want.
It is an open feature request to have I/O errors raise an exception but I am not aware that it is under active development.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
An approach we use to report errors in a Fortran DLL back to the calling routine (such as C# or Visual Basic) is to trap the error in the Fortran DLL and then return an integer value along with a string with a useful message.This usually means adding more checks in the Fortran code to trap possible errors. For example, if the DLL runs without an error then the integer is returned as zero, but if an error occurs in the DLL then the integer returns a positive value (this is like Windows API routines). If there is an error, it can be helpful to return a message in the string to suggest a change to the input values, then try running again. An advantage is that the programmay not need to exit and restarted to change the input and rerun the Fortran DLL. Perhaps this approach would work for your C# program to check the value of the error integer returned from the DLL, then raise the error from C#.
Regards,
Greg
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Greg
When you trap the error, is there a way to immediately exit the DLL with a string or do you have to return-all-the-way back out?
I have a large program, a version of which has been made into a DLL that is called by C#. It has a lot of trapping, but the trap prints out a message to a log file followed by a stop. So say the DLL is thirty subroutines deep and encounters a trap, can I exit immediately whilereturning a string to the C# routine? Or do I have to manually add an exception handlingafter every call statement?
foo_a (arguments, is_exception, message)
call foo_b (arguments, is_exception, message)
if (is_exception) then
return
else
do normal run
endif
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
Our DLLs are set up to pass the integer variable (the error flag) and the message string all the way back out to the calling program. I don't know of a way to immediately exit the DLL from deep in the call stack. If there is a way it would be nice to know more about it.
For many subroutines down the call stack, our DLLs return (exit)from each subroutine when the error flag is set. That does require extra code in every subroutine to check if the error code has been set. I usually use an if-statement after each subroutine callto check for the error code, if it is set then either return immediately, or use a "goto" to clean up any locally allocated arrays. This is the oneexample where I still use a "goto" statement, since it allows a jump to the end of the routine where all the deallocation statements can be located. I use the goto the end of the subroutineas a way to "raise and error" in Fortran. For a regular Fortran program I don't think deallocation is always needed since the program executionjust ends, but in this case we want to exit a DLL, allow for changes to the input, and call the DLL again without closing the calling program, the extra error trapping and local array deallocation has been necessary for us. If there are other ways to reenter a DLL, I would like to know.
For older code we may add a module with the error code integer and string variables, that way we don't need to change all the call statements of many subroutines, but just add the "use
It sounds to me like you could still write the message to a text file, set and return just the error code integerand exit the DLL, but don't use the "stop". Maybe the C# code could obtain the string from the text file if the error code is returned before it raises the error, then include the DLL message in the raised error? It is probably cleaner styleto pass the string back, but could take some effort to get the message from deep in the DLL all the way back to the C# program.
Or, you could use the Windows API routine that Steve suggested as a way to set and error and check for it in the C# program, if that would be easier to implement.
Regards,
Greg

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page