| 1 |
.. title:: clang-tidy - google-objc-avoid-throwing-exception |
| 2 |
|
| 3 |
google-objc-avoid-throwing-exception |
| 4 |
==================================== |
| 5 |
|
| 6 |
Finds uses of throwing exceptions usages in Objective-C files. |
| 7 |
|
| 8 |
For the same reason as the Google C++ style guide, we prefer not throwing |
| 9 |
exceptions from Objective-C code. |
| 10 |
|
| 11 |
The corresponding C++ style guide rule: |
| 12 |
https://google.github.io/styleguide/cppguide.html#Exceptions |
| 13 |
|
| 14 |
Instead, prefer passing in ``NSError **`` and return ``BOOL`` to indicate success or failure. |
| 15 |
|
| 16 |
A counterexample: |
| 17 |
|
| 18 |
.. code-block:: objc |
| 19 |
|
| 20 |
- (void)readFile { |
| 21 |
if ([self isError]) { |
| 22 |
@throw [NSException exceptionWithName:...]; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
Instead, returning an error via ``NSError **`` is preferred: |
| 27 |
|
| 28 |
.. code-block:: objc |
| 29 |
|
| 30 |
- (BOOL)readFileWithError:(NSError **)error { |
| 31 |
if ([self isError]) { |
| 32 |
*error = [NSError errorWithDomain:...]; |
| 33 |
return NO; |
| 34 |
} |
| 35 |
return YES; |
| 36 |
} |
| 37 |
|
| 38 |
The corresponding style guide rule: |
| 39 |
https://google.github.io/styleguide/objcguide.html#avoid-throwing-exceptions |