8. Exception raising utilities#

In the process of writing

The UTEXCP and UTEXCM utilities allow you to raise a Python exception from Fortran and pass it to the command set. This exception can be retrieved from the command file (or a macro command) by a try/except block.

Exceptions are specific cases of error<F>, if they are not recovered, the supervisor finishes the calculation as after an <F>ordinary error (closing and copying the database). Only the FatalError exception escapes the rule since it is equivalent to an error <F>(and again we will see later that this behavior is modifiable).

The exception mechanism allows you to try a command and then regain control if it fails by raising a particular exception.

This operation is particularly used in macro-commands, either to behave differently depending on the context, or to send a message that is more meaningful to the user than that which would have been sent by a daughter command.

Note:

The use of exceptions and especially blocks try/except only makes sense in mode PAR_LOT =” NON “

8.1. Implementation in the source#

Here we present the implementation of the exception mechanism in Fortran and Python source from a developer perspective. Exceptions will be discussed below with the « user » vision (§ 8.4).

For the description of what an exception is, the mechanisms for raising (raising) and interception (except), refer to the Exception Description in the Python Tutorial.

« Throwing an exception » means sending a signal with (often) context elements to allow the calling program to react as follows:

  • I understand the signal and I am making this decision accordingly

  • I don’t understand the signal and I stop (by sending another signal to the calling program at the higher level)

  • I ignore the signal and let the higher-level calling program process the signal

And so on, knowing that at the very top, it is the Python interpreter itself that will make the decision to stop the execution.

Syntactically and schematically, the operation is as follows:

Try:


...

Instructions to execute that can for example raise 3 exceptions

different: A Special Exception, Another Exception, Another Exception

...


except for one particular exception, arguments:


...

These instructions will be executed if the "A Particular Exception" exception is raised. You can use the "arguments" used when raising the exception.

In the simplest (and most common) case, "arguments" is a message that can be printed with "print" or used as a character string with "str (arguments)".

...


except for another Exception, arguments 2:


...

Instructions to be executed if the AntherException exception is raised.

...

Assume that the instruction block under the try throws the ExceAnother exception (which is not caught by an except block), the execution stops with a message like:

Traceback (most recent call last):

File" ... ", line 3, in <... > <<< allows you to say where the exception occurred

EntryAnother:... <<< context elements (arguments) providing

Details about the reasons Of the lifting of exceptions

For the other mechanisms, try/finally or try/except/else, refer to the Python documentation.


8.1.1. Exception raised in Code_Aster#

The simplest: in Python, that is to say in the command file or in a macro command, all you have to do is:

if nbre_iterations > nbre_maxim_allows:

raise aster.NonConvergenceError, "Lack of convergence with the allowed number of iterations"

In the fortran source (example in nmerro.f that treats errors in STAT_NON_LINE):

...

ELSE IF (ITEMAX) THEN

CALL UTEXCP (22, 'MECANONLINE_83')

...

where

  • 22 is the exception number

  • “MECANONLINE_83” is the id of the message to be printed

Indeed, the exceptions specific to Code_Aster are numbered to be easily accessible from Fortran:


exception number

Description

Exception name in the aster module

Error type

20

Fatal error

FatalError

F

21

Non-fatal error

error

F

22

Non convergence

NonconvergenceError

F

23

Failed behavior integration

EchecbehaviorError

F

24

Empty Frequency Band

BandeFrequenceVideError

F

25

Singular matrix

matriceSinguliereError

F

26

Contact processing failed

ProcessContactError

F

27

Non-invertible contact matrix

matriceContactSingularyError

F

28

Lack of time CPU

Stop CPUError

F

29

Piloting failed

PilotageError

F


Remarks

Exceptions are defined in the aster module (astermodule.c). So, it is accessed by aster.error.

<F>*Apart from FatalError, all exceptions derive from error, which therefore represents the error envelope.*

The behavior in case of a fatal error (FatalError) can be modified by the user [U1.03.01], this choice is determined in the commands DEBUT/POURSUITE [U4.11.01], [U4.11.03] keyword factor ERREUR_F. In macro commands, we’ll use the aster.onFatalError (arg) function to change this behavior.

8.2. Raising an exception and printing a message#

CALL UTEXCP (CODE, IDMESS)

CODE

IN

Exception number

IDMESS

IN

Message ID

Example:

CALL UTEXCP (23, “CHATON_7”)

8.3. Exception raising with printing messages and character values, integers and real values#

CALL UTEXCM (CODE, IDMESS, NK, VALK,, NI, NI, VALI, NR, VALR)

CODE

IN

exception number

IDMESS

IN

Message ID

NK

IN

Number of character parameters

VALK

IN

Character parameter values

NI

IN

Number of integer parameters

VALI

IN

Integer parameter values

NR

IN

Number of real type parameters

VALR

IN

Real type parameter values

Example:

VALI (1) = NDIM1

VALI (2) = NDIM2

VALR = 5.D0

CALL UTEXCM (24, 'CHATON_12', 0, 0, ',' ',2, VALI ,1, VALR)

8.4. Example of use#

In this example, we want to:

  • in case of non-convergence of STAT_NON_LINE, restart the calculations by increasing the number of iterations,

  • stop the calculations if another error occurs.

This use is illustrated in the ssnop125a test case.


Programming

We raise the NonConvergenceError exception with the number 22:

...

ELSE IF (. NOT. CONVER). AND. ITEMAX. AND. (. NOT. ARRET)) THEN

ITAB (1) = NUMORD

ITAB (2) = ITERAT

CALL UTEXCM (22, 'MECANONLINE_85', 0, K8B, 2, ITAB, 0, RTAB)

END IF

Order file

Try:

STATNL = STAT_NON_LINE (...)


except aster.NonConvergenceError, message:

# non convergence

print "we continue by increasing the number of iterations"

STATNL = STAT_NON_LINE (reuse= STATNL,

...

CONVERGENCE =_F (ITER_GLOB_MAXI =400,)

...)

Message file: mecanonline.py

85: _ (u" ""

Stop: lack of convergence at the instant number:% (i1) d

during iteration:% (i2) d

"""),

Printed message

! --------------------------------------------------------!

! <EXCEPTION> <MECANONLINE_85>!

! !

! !

! Stop: no convergence at the moment number: 123!

! during iteration: 51!

! !

! !

! --------------------------------------------------------!