rethrow

Rethrows an exception that was caught in a catch block.

rethrow

Return Values

After the rethrow statement, $procerror has the value of the error and $procerrorcontext contains a copy of the call stack information of the error that was caught in the current catch block.

Use

Allowed in all component types.

Description

The rethrow statement can be used to rethrow the same exception that was just caught in a try-catch block. This is useful when you want to catch an exception but handle it elsewhere. For example, to clean up after an error, then pass the exception to the caller to handle or log it.

The rethrown exception is a copy of the original exception, and contains the same call stack information as the original exception, allowing you to identify the cause of the exception.

The rethrow statement can only be used inside a catch block, as it throws an exception that has already been caught. If you use rethrow elsewhere, the compiler reports the following error:

error: 1000 - "rethrow" statement only allowed inside a catch block.

Note: rethrow cannot be used inside a try or finally block.

If a finally block is present, this will be executed immediately after the rethrow statement. After finally has been executed, the exception will bubble up to a higher level until it is handled by a catch block or results in an uncaught exception.

Some ProcScript statements, such as clear or reset, can reset $procerror and $procerrorcontext. If a rethrow statement is used after such a ProcScript command, the caught exception is still rethrown, with its original $procerror and $procerrorcontext.

Example

When catching a range of errors, it can be useful to handle specific errors and rethrow the exception if any other error is returned, so that it can bubble up to the next try block. For example:

entry my_entry
  throws
  try
    ... ; Code that might raise system exceptions or custom exceptions
  catch -2, -4, -10050
    ... ; Handle these IO and custom errors
  catch  ; Plain catch-block that handles all other errors
    if ($procerror >= -150 & $procerror < -200)
      ; These are exceptions related to component activation.
      ; Pass them on to caller
      rethrow   
    endif
    ; Other exceptions are ignored.
  endtry
end

Related Topics