visitRethrowExpression method

  1. @override
Object? visitRethrowExpression(
  1. SRethrowExpression node
)
override

Implementation

@override
Object? visitRethrowExpression(SRethrowExpression node) {
  final asyncState = currentAsyncState;
  if (asyncState == null) {
    if (!_isInCatchBlock ||
        _originalCaughtInternalExceptionForRethrow == null) {
      throw RuntimeD4rtException(
        "'rethrow' can only be used within a catch block.",
      );
    }
    Logger.debug(
      "[Rethrow] Rethrowing original internal exception: ${_originalCaughtInternalExceptionForRethrow!.originalThrownValue}",
    );
    // Re-launch the *original internal exception* that was caught by the enclosing catch block
    throw _originalCaughtInternalExceptionForRethrow!;
  }
  if (!asyncState.isHandlingErrorForRethrow) {
    throw RuntimeD4rtException(
      "'rethrow' can only be used within a catch block.",
    );
  }
  final originalError = asyncState.originalErrorForRethrow;
  if (originalError == null) {
    // Should not happen if isHandlingErrorForRethrow is true, but safety check
    throw StateD4rtException(
      "Internal error: Inconsistent state for rethrow.",
    );
  }
  Logger.debug(
    "[Rethrow] Rethrowing original internal exception: ${originalError.originalThrownValue}",
  );
  // Set flag to indicate this is a rethrow, not a new exception
  asyncState.isCurrentlyRethrowing = true;
  // Relaunch the original exception stored in the async state
  throw originalError;
}