Back to all reviewers

ensure exception safety

facebook/react-native
Based on 3 comments
Other

Always ensure proper exception safety by placing cleanup code in @finally blocks, using stack-allocated exceptions, and implementing comprehensive exception handling for different types. Resource cleanup must occur even when exceptions are thrown to prevent memory leaks.

Error Handling Other

Reviewer Prompt

Always ensure proper exception safety by placing cleanup code in @finally blocks, using stack-allocated exceptions, and implementing comprehensive exception handling for different types. Resource cleanup must occur even when exceptions are thrown to prevent memory leaks.

Key practices:

  • Place cleanup code like [retainedObjectsForInvocation removeAllObjects] in @finally blocks, not after catch blocks
  • Use throw std::runtime_error(...) instead of throw new std::runtime_error(...) for C++ exceptions
  • Implement comprehensive exception handling for different types (NSException, NSError, NSString, std::exception, etc.)

Example:

@try {
  [inv invokeWithTarget:strongModule];
} @catch (NSException *exception) {
  caughtException = maybeCatchException(shouldCatchException, exception);
} @catch (NSError *error) {
  caughtException = maybeCatchException(shouldCatchException, error);
} @finally {
  [retainedObjectsForInvocation removeAllObjects]; // Always cleanup
}

// For C++ exceptions, use stack allocation
throw std::runtime_error("Error message");

This ensures robust error handling while preventing resource leaks and maintaining proper exception semantics.

3
Comments Analyzed
Other
Primary Language
Error Handling
Category

Source Discussions