If an exception is already being thrown within the try
block or caught in a catch
block, throwing another exception in
the finally
block will override the original exception. This means that the original exception’s message and stack trace will be lost,
potentially making it challenging to diagnose and troubleshoot the root cause of the problem.
void openResource() {
@throw [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil];
}
void fun() {
@try {
openResource();
}
@finally {
closeResource();
@throw ... ; // Noncompliant - will mask previous exception
}
}
void openResource() {
@throw [NSException exceptionWithName:@"FileNotFoundException" reason:@"File Not Found on System" userInfo:nil];
}
void fun() {
@try {
openResource();
}
@finally {
closeResource();
}
}