Question

Is there a method that is similar to the try catch in php?

try {
    ...
} 
catch (Exception $e) {
    ...
} 

I already use the blocks of this type: DO ON ERROR UNDO ... but I wonder if there is another way to do it.

Thanks.

Was it helpful?

Solution

Yes. Since version 11.2 there's the BLOCK-LEVEL ON ERROR UNDO, THROW. statement that changes the default behavior of your entire program. CATCH has been around longer - since version 10.1C.

/* Must be first line of program */
BLOCK-LEVEL ON ERROR UNDO, THROW.

DEFINE VARIABLE i AS INTEGER     NO-UNDO.

i = INTEGER("Hello").

CATCH err AS Progress.Lang.Error:    
    MESSAGE "Error: " err:GetMessage(1)        
        VIEW-AS ALERT-BOX ERROR.
END.

Older style program with do on error like your example, taken from the Progress KnowledgeBase.

DO ON ERROR UNDO, THROW:
  /* The following line raises error (138) which is diverted to a Progress.Lang.SysError object and thrown to the main block. */ 
  FIND Customer 1000.
END.
 
CATCH eAnyError AS Progress.Lang.Error:
  MESSAGE
      "Error Number:~t" eAnyError:GetMessageNum(1) "~n"
      "Error Text:~t" eAnyError:GetMessage(1)
      VIEW-AS ALERT-BOX BUTTONS OK TITLE "Error processing in the CATCH for mainprocedure block".
END CATCH.

Complete description of BLOCK-LEVEL ON ERROR UNDO, THROW

From the manual:

Syntax BLOCK-LEVEL ON ERROR UNDO, THROW.

This statement affects the following block types:

Main block of an external procedure (.p)

Internal procedures

User-defined functions

Methods of a class

Class constructors

Property accessors

ON blocks used as database triggers with CREATE, DELETE, WRITE or ASSIGN events

REPEAT blocks

FOR blocks

DO TRANSACTION blocks

This statement does not affect:
Destructors

Error directives that are explicitly coded in individual, non routine-level blocks

ON blocks that are UI triggers.

Note these alternatives to the BLOCK-LEVEL ON ERROR UNDO, THROW statement:
Instead of adding the statement to source-code files, you can use the -undothrow 2 startup parameter to change the default error-handling to UNDO, THROW on every block affected by the BLOCK-LEVEL statement during compilation. See the Startup Command and Parameter Reference for more information.

The ROUTINE-LEVEL ON ERROR UNDO, THROW statement can be used if you want to change the default error-handling only on routine-level blocks. (You can use the -undothrow 1 startup parameter to change the default error-handling on routine-level blocks to UNDO, THROW during compilation.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top