Exception Handling

RSS for tag

Monitor and debug exceptional conditions in code using Exception Handling.

Posts under Exception Handling tag

5 Posts

Post

Replies

Boosts

Views

Activity

What is an exception?
The word exception is highly overloaded, not just on Apple platforms but across the industry as a whole. From an Apple perspective there are 3 things that are commonly conflated under that term: Machine exceptions — These are raised by the hardware in response to problems detected by the hardware, for example, accessing invalid memory, executing an illegal instruction, and executing a trap instruction. Language exceptions — This includes Objective-C (@try, @catch, @throw) and C++ exceptions (try, throw, catch) Cocoa errors Catching machine exceptions is super hard and it only makes sense in very specific circumstances, for example, when you’re working on a language runtime. IMPORTANT Folks commonly try to catch machine exceptions as part of a custom crash reporter. I strongly recommend against doing that, for the reasons I outline in Implementing Your Own Crash Reporter - https://developer.apple.com/forums/thread/113742. The Exception Handling framework - https://developer.apple.com/documentation/exceptionhandling, which is the tag that I applied to this post (-:, lets you convert machine exceptions to language exceptions. This is dangerous nonsense and should never be used. The situation with language exceptions varies by language: In C++ it’s common to use language exceptions as part of your program. In Objective-C language exceptions are reserved for serious programming errors. Do not throw a language exception unless you want your program to crash. Do not attempt to catch a language exception and then recover from it. Doing so will not work reliably if you’re using ARC or if the language exception originated in the OS. Swift has no facilities for dealing with language exceptions. The exception-like mechanisms you see in Swift are actually syntactic sugar on the Cocoa error facilities (more on that below). In no situation is it safe to throw or catch a language exception across an ABI boundary. Note Historically some Cocoa APIs expected you to catch language exceptions. These APIs are now either deprecated (for example, Distributed Objects) or have been replaced by APIs that use the Cocoa error mechanism (for example, NSFileManager). The Cocoa error mechanism involves a function that returns a status result and can optionally return an NSError via an ‘out’ parameter. In Objective-C this is done using an NSError ** parameter. For example, to read an NSData from a file you use this NSData method: (nullable instancetype)dataWithContentsOfURL:(NSURL *)url options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr; To see the error in Objective-C, call it like so: NSURL * url = … something …; NSError * error; NSData * data = [NSData dataWithContentsOfURL:url options:0 error:&error]; if (data == nil) { … look at `error` … } IMPORTANT Only look at error if the function result indicates that an error occurred. Swift has syntactic sugar to make this look like an exception mechanism. For example, in Swift you’d call it like so: let url: URL = … something … let data = try Data(contentsOf: url: options:[]) While this looks like an exception, it’s not. Rather, it’s a convenient way to handle the existing Cocoa error convention. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
0
0
4.5k
May ’21
Debugging Resources
General: DevForums tags: Debugging, LLDB, Graphical Debugger Xcode > Debugging documentation Diagnosing memory, thread, and crash issues early documentation Diagnosing issues using crash reports and device logs documentation Choosing a Network Debugging Tool documentation Testing a release build documentation Isolating Code Signing Problems from Build Problems DevForums post What is an exception? DevForums post Language Exception from RCTFatal DevForums post Standard Memory Debugging Tools DevForums post Using a Sysdiagnose Log to Debug a Hard-to-Reproduce Problem DevForums post Posting a Crash Report DevForums post Creating a test project DevForums post Implementing Your Own Crash Reporter DevForums post Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
0
0
6.6k
Aug ’24
Integrating BugSplat Crash Reporter
Hello, For the past few days I have been working on integrating a Crash Reporter called "BugSplat" to our macOS desktop app and it got me thinking, how do crash reporters work on macOS ? After days of trial and error (and claude's help) I managed to integrate it but with a different behavior than windows. On Windows: When the app crashes, the BugSplat crash report window opens and allows you to write your details and message to be sent with the crash report (along with a log file) On macOS: It displays the normal macOS' "App exit unexpectedly", to which I click dismiss. But then when I re-open the app the BugSplat crash report window appears. I asked claude if I can avoid the normal macOS dialog entirely, to which it replied: "This is by design — BugSplat macOS is a next-launch crash reporter, not an in-process one. Here's why that's intentional and unavoidable: When a process crashes (SIGSEGV, SIGBUS, etc.), macOS's ReportCrash daemon independently monitors all processes and shows the "quit unexpectedly" dialog — there's no API to suppress it. Meanwhile, BugSplat's Mach exception handler captures the crash data to disk. On the next launch, BugSplat finds that crash data and shows its dialog. This is the same model used by every macOS crash reporter (PLCrashReporter, Firebase Crashlytics on macOS, etc.). The process heap is in a corrupted state at crash time, so showing UI from inside the crashed process is unreliable." LLMs sometimes speak confidently even when they're wrong so that's why I'm asking: Is it really true that that's the normal behavior for every crash reporter? And is it really that huge of a change to overwrite the macOS dialog entirely ? Thank you in advance and sorry for the long paragraph
1
0
61
3d
After upgrading to iOS 18, crashes caused by calling null function pointers cannot be captured by developers using signal listeners.
After upgrading to iOS 18, crashes caused by calling null function pointers have changed their crash signal from SIGEGV to SIGKILL, making it impossible for developers to capture crash stacks using third-party components like KSCrash/PLCrashReporter. Is this a design change in iOS 18's memory protection mechanism? If so, are there any officially recommended crash capture solutions? - (void)MockCrashOnNullFunctionPointer { void (*func)(void) = NULL; func(); } Crash report comparison:
2
0
141
Dec ’25
After upgrading to iOS 18, crashes caused by calling null function pointers cannot be captured by developers using signal listeners.
After upgrading to iOS 18, crashes caused by calling null function pointers have changed their crash signal from SIGEVG to SIGKILL, making it impossible for developers to capture crash stacks using third-party components like KSCrash/PLCrashReporter. Is this a design change in iOS 18's memory protection mechanism? If so, are there any officially recommended crash capture solutions? Crash example code: - (void)MockCrashOnNullFunctionPointer { void (*func)(void) = NULL; func(); } Crash report comparison:
3
0
92
Dec ’25
Xcode 26 Debug Error Display Issue
I recently installed Xcode 26.0.1. (MacBook Pro 16, M2 Max, 64GB memory, macOS 26.0 (25A354)) Normally, when debugging, a red error appears in the left-hand Issues navigation, and you can click on it to access the location of the error. However, currently, when debugging, the red error does not appear in the "Issue Navigation" on the left-hand side of the Xcode screen. There is an error, but it isn't displayed. It does appear, but disappears after 1 second. (It disappears before I can click on it.) The error doesn't appear or disappears after 1 second, so I can't pinpoint the exact location. Please help me resolve this issue. Additionally, I deleted all Xcode development-related files inside my Mac and restarted my MacBook, but the symptom still persists.
3
1
303
Sep ’25
What is an exception?
The word exception is highly overloaded, not just on Apple platforms but across the industry as a whole. From an Apple perspective there are 3 things that are commonly conflated under that term: Machine exceptions — These are raised by the hardware in response to problems detected by the hardware, for example, accessing invalid memory, executing an illegal instruction, and executing a trap instruction. Language exceptions — This includes Objective-C (@try, @catch, @throw) and C++ exceptions (try, throw, catch) Cocoa errors Catching machine exceptions is super hard and it only makes sense in very specific circumstances, for example, when you’re working on a language runtime. IMPORTANT Folks commonly try to catch machine exceptions as part of a custom crash reporter. I strongly recommend against doing that, for the reasons I outline in Implementing Your Own Crash Reporter - https://developer.apple.com/forums/thread/113742. The Exception Handling framework - https://developer.apple.com/documentation/exceptionhandling, which is the tag that I applied to this post (-:, lets you convert machine exceptions to language exceptions. This is dangerous nonsense and should never be used. The situation with language exceptions varies by language: In C++ it’s common to use language exceptions as part of your program. In Objective-C language exceptions are reserved for serious programming errors. Do not throw a language exception unless you want your program to crash. Do not attempt to catch a language exception and then recover from it. Doing so will not work reliably if you’re using ARC or if the language exception originated in the OS. Swift has no facilities for dealing with language exceptions. The exception-like mechanisms you see in Swift are actually syntactic sugar on the Cocoa error facilities (more on that below). In no situation is it safe to throw or catch a language exception across an ABI boundary. Note Historically some Cocoa APIs expected you to catch language exceptions. These APIs are now either deprecated (for example, Distributed Objects) or have been replaced by APIs that use the Cocoa error mechanism (for example, NSFileManager). The Cocoa error mechanism involves a function that returns a status result and can optionally return an NSError via an ‘out’ parameter. In Objective-C this is done using an NSError ** parameter. For example, to read an NSData from a file you use this NSData method: (nullable instancetype)dataWithContentsOfURL:(NSURL *)url options:(NSDataReadingOptions)readOptionsMask error:(NSError **)errorPtr; To see the error in Objective-C, call it like so: NSURL * url = … something …; NSError * error; NSData * data = [NSData dataWithContentsOfURL:url options:0 error:&error]; if (data == nil) { … look at `error` … } IMPORTANT Only look at error if the function result indicates that an error occurred. Swift has syntactic sugar to make this look like an exception mechanism. For example, in Swift you’d call it like so: let url: URL = … something … let data = try Data(contentsOf: url: options:[]) While this looks like an exception, it’s not. Rather, it’s a convenient way to handle the existing Cocoa error convention. Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
Replies
0
Boosts
0
Views
4.5k
Activity
May ’21
Debugging Resources
General: DevForums tags: Debugging, LLDB, Graphical Debugger Xcode > Debugging documentation Diagnosing memory, thread, and crash issues early documentation Diagnosing issues using crash reports and device logs documentation Choosing a Network Debugging Tool documentation Testing a release build documentation Isolating Code Signing Problems from Build Problems DevForums post What is an exception? DevForums post Language Exception from RCTFatal DevForums post Standard Memory Debugging Tools DevForums post Using a Sysdiagnose Log to Debug a Hard-to-Reproduce Problem DevForums post Posting a Crash Report DevForums post Creating a test project DevForums post Implementing Your Own Crash Reporter DevForums post Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
Replies
0
Boosts
0
Views
6.6k
Activity
Aug ’24
Integrating BugSplat Crash Reporter
Hello, For the past few days I have been working on integrating a Crash Reporter called "BugSplat" to our macOS desktop app and it got me thinking, how do crash reporters work on macOS ? After days of trial and error (and claude's help) I managed to integrate it but with a different behavior than windows. On Windows: When the app crashes, the BugSplat crash report window opens and allows you to write your details and message to be sent with the crash report (along with a log file) On macOS: It displays the normal macOS' "App exit unexpectedly", to which I click dismiss. But then when I re-open the app the BugSplat crash report window appears. I asked claude if I can avoid the normal macOS dialog entirely, to which it replied: "This is by design — BugSplat macOS is a next-launch crash reporter, not an in-process one. Here's why that's intentional and unavoidable: When a process crashes (SIGSEGV, SIGBUS, etc.), macOS's ReportCrash daemon independently monitors all processes and shows the "quit unexpectedly" dialog — there's no API to suppress it. Meanwhile, BugSplat's Mach exception handler captures the crash data to disk. On the next launch, BugSplat finds that crash data and shows its dialog. This is the same model used by every macOS crash reporter (PLCrashReporter, Firebase Crashlytics on macOS, etc.). The process heap is in a corrupted state at crash time, so showing UI from inside the crashed process is unreliable." LLMs sometimes speak confidently even when they're wrong so that's why I'm asking: Is it really true that that's the normal behavior for every crash reporter? And is it really that huge of a change to overwrite the macOS dialog entirely ? Thank you in advance and sorry for the long paragraph
Replies
1
Boosts
0
Views
61
Activity
3d
After upgrading to iOS 18, crashes caused by calling null function pointers cannot be captured by developers using signal listeners.
After upgrading to iOS 18, crashes caused by calling null function pointers have changed their crash signal from SIGEGV to SIGKILL, making it impossible for developers to capture crash stacks using third-party components like KSCrash/PLCrashReporter. Is this a design change in iOS 18's memory protection mechanism? If so, are there any officially recommended crash capture solutions? - (void)MockCrashOnNullFunctionPointer { void (*func)(void) = NULL; func(); } Crash report comparison:
Replies
2
Boosts
0
Views
141
Activity
Dec ’25
After upgrading to iOS 18, crashes caused by calling null function pointers cannot be captured by developers using signal listeners.
After upgrading to iOS 18, crashes caused by calling null function pointers have changed their crash signal from SIGEVG to SIGKILL, making it impossible for developers to capture crash stacks using third-party components like KSCrash/PLCrashReporter. Is this a design change in iOS 18's memory protection mechanism? If so, are there any officially recommended crash capture solutions? Crash example code: - (void)MockCrashOnNullFunctionPointer { void (*func)(void) = NULL; func(); } Crash report comparison:
Replies
3
Boosts
0
Views
92
Activity
Dec ’25
Xcode 26 Debug Error Display Issue
I recently installed Xcode 26.0.1. (MacBook Pro 16, M2 Max, 64GB memory, macOS 26.0 (25A354)) Normally, when debugging, a red error appears in the left-hand Issues navigation, and you can click on it to access the location of the error. However, currently, when debugging, the red error does not appear in the "Issue Navigation" on the left-hand side of the Xcode screen. There is an error, but it isn't displayed. It does appear, but disappears after 1 second. (It disappears before I can click on it.) The error doesn't appear or disappears after 1 second, so I can't pinpoint the exact location. Please help me resolve this issue. Additionally, I deleted all Xcode development-related files inside my Mac and restarted my MacBook, but the symptom still persists.
Replies
3
Boosts
1
Views
303
Activity
Sep ’25
Crash Report - What may have been the cause?
See crash details here:- https://pastebin.com/i9u5PE4X There's a comprehensive thread here, folks! https://discussions.apple.com/thread/255651156?sortBy=oldest_first Thanks for any thoughts.
Replies
11
Boosts
0
Views
1.6k
Activity
Aug ’25