Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Error handling

OP1
New Contributor III
369 Views
Let's assume I have a DLL which exports the following subroutine:

[fortran]SUBROUTINE MY_MAIN_SUB(I,J,STATUS)
!DEC$ ATTRIBUTES DLLEXPORT :: MY_MAIN_SUB
! ... do a lot of work here ...
END SUBROUTINE MY_MAIN_SUB [/fortran]

I,J are input/output parameters; STATUS is a flag indicating successful call to the subroutine or not.

This subroutine calls many other internal subroutines in this DLL. Now, if an error condition occurs (and is detected) in one of these internal subroutines, is there a convenient way to come back to the main subroutine toset my flag STATUS to -1 for instance? Obviously I canpass STATUS as an argument for all my internal subroutines, and then use a RETURN statement in each of these, but there are many, many of them...

Is there another way around this tedious process?

Thanks,

Olivier
0 Kudos
5 Replies
Paul_Curtis
Valued Contributor I
369 Views
Presumably all your DLL routines are in a module, which can have its own status flag (distinct from the STATUS passed output argument) with modular scope, call it mod_status, which only needs to be defined once so you don't have to push it on the stack as an added argument for all subroutine calls. To track errors (each with its own mod_status value), every call to an internal DLL routine would be immediately followed by
IF (mod_status /= no_error) RETURN
which would clean out the call stack all the way back to the entry routine, whereupon STATUS = mod_status would be set on exit. This provides a nice way to improve on simply "an error occured" by returning the detailed error type or locus.
0 Kudos
Nick2
New Contributor I
369 Views
Depending on how comfortable you are using WinAPI routines ormixing C/C++ and Fortran, you can try something like this:


[cpp]void CallTheFortranFunction()
{
	bool bDone=false;
	CONTEXT c;
	RtlCaptureContext (&c);
	if(!bDone)
	{
		bDone=true;
		FortranFunction();
		// To quickly abort FortranFunction, have it call
		// SetThreadContext(GetCurrentThread(),&c);
	}
}[/cpp]
0 Kudos
Paul_Curtis
Valued Contributor I
369 Views
Yes, that's clever; I do something very similar to wind up communications threads. However, SetThreadContext() calls would have to be added at each error check, and the amount of work, both in recoding and in runtime, would not be less than simply using a module-scoped flag with a RETURN chain.
0 Kudos
Nick2
New Contributor I
368 Views
Very true. The only wayI found to somewhat"automate" the process is to deliberately let exceptions be raised and handled.
0 Kudos
OP1
New Contributor III
368 Views
Thanksfor the replies. It looks like I am not going to avoid having to edit all these subroutines after all. The solution I have is close to what Paul suggested.

Thanks again!

Olivier
0 Kudos
Reply