Exception Handling

RSS for tag

Monitor and debug exceptional conditions in code using Exception Handling.

Posts under Exception Handling tag

10 Posts
Sort by:

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
3.7k
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
3.3k
Aug ’24
Trouble Seeing print debug statements
I am working on an XCode project and I have some Swift functions within a helper functions file. This file has some functions declared that create 3 global variables. These global variables are then used in various views throughout the app. In the functions, there are print statements for when it catches an error. However, when I run the app and I get an error that stems from this file, there are no print statements. How can I have this print statements show up in the debug console?
1
0
81
1d
Does SystemMemoryReset related to my Application abnormal exit?
Hello, we are currently developing a VPN application. Recently, we have encountered several cases where the Network Extension process terminates unexpectedly. We cannot find any related crash logs on the device, but we can find system SystemMemoryReset logs. The timestamps in these logs closely match (with millisecond accuracy) the times when our VPN process terminated unexpectedly. We have a few questions: 1.How is the SystemMemoryReset event generated, and can this event be avoided? 2.When a SystemMemoryReset occurs, can it cause our VPN background process to be killed or the system to reboot? 3.If the background process can be killed, what conditions need to be met for this to happen, and what methods can we use to prevent the VPN background process from being killed? 4.Based on these logs, does our application have any related issues (the process name is CorplinkTunnel)? Do you have any suggestions for modifications? SystemMemoryReset-2024-06-25-232108.log SystemMemoryReset-2024-06-29-025353.log SystemMemoryReset-2024-07-01-024655.log
1
0
703
Jul ’24
App crashed with NSInvalidUnarchiveOperationException when run from different target and scheme.
Hi, I'm trying to create a new target duplicated from the main target (cdx_ios) called cdx-ios-dev02. I also made a new scheme called cdx-ios-dev02 It can be built just fine however when I run it, it crashed and it throws this exception: *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (cdx_ios.AuthObject) for key (root) because no class named "cdx_ios.AuthObject" was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target). If the class was renamed, use setClassName:forClass: to add a class translation mapping to NSKeyedUnarchiver' This is the class: class AuthObject: NSObject, NSCoding { var accessT1: String = "" var t1Type: String = "bearer" var refreshT1: String = "" var expiresIn: Int = 0 var scope: String = "" var jti: String = "" init(accessT1: String = "", t1Type: String = "bearer", refreshT1: String = "", expiresIn: Int = 0, scope: String = "", jti: String = "") { self.accessT1 = accessT1 self.t1Type = t1Type self.refreshT1 = refreshT1 self.expiresIn = expiresIn self.scope = scope self.jti = jti } convenience init(dic: [String: Any]) { self.init() mapping(dic) } required convenience init(coder aDecoder: NSCoder) { let t1 = aDecoder.decodeObject(forKey: "accessT1") as? String ?? "" let t1Type = aDecoder.decodeObject(forKey: "t1Type") as? String ?? "" let refreshT1 = aDecoder.decodeObject(forKey: "refreshT1") as? String ?? "" let expiresIn = aDecoder.decodeInteger(forKey: "expiresIn") let scope = aDecoder.decodeObject(forKey: "scope") as? String ?? "" let jti = aDecoder.decodeObject(forKey: "jti") as? String ?? "" self.init( accessT1: t1, t1Type: t1Type, refreshT1: refreshT1, expiresIn: expiresIn, scope: scope, jti: jti ) } func mapping(_ dic: [String: Any]) { accessT1 = ParseUtil.dictionaryValue(dic, "access_token", "") t1Type = ParseUtil.dictionaryValue(dic, "token_type", "bearer") refreshT1 = ParseUtil.dictionaryValue(dic, "refresh_token", "") expiresIn = ParseUtil.dictionaryValue(dic, "expires_in", 0) scope = ParseUtil.dictionaryValue(dic, "scope", "") jti = ParseUtil.dictionaryValue(dic, "jti", "") } func encode(with nsCoder: NSCoder) { nsCoder.encode(accessT1, forKey: "accessT1") nsCoder.encode(t1Type, forKey: "t1Type") nsCoder.encode(refreshT1, forKey: "refreshT1") nsCoder.encode(expiresIn, forKey: "expiresIn") nsCoder.encode(scope, forKey: "scope") nsCoder.encode(jti, forKey: "jti") } } It worked fine on the original target, cdx-ios. Can anybody help me? Thank you.
3
0
706
Jun ’24
CoreML Crashed in iOS18 Beta
Here is an App using CoreML API with ML package format, it works fine in iOS17, while it is crashed when calling [MLModel modelWithContentsOfURL ] to load model running in iOS18. It seems an exception is raised "Failed to set compute_device_types_mask E5RT: Cannot provide zero compute device types. (1)". Is it a bug of iOS18 beta version , and it will be fixed in the future? The stack is as below: Exception Codes: #0 at 0x1e9280254 Crashed Thread: 49 Application Specific Information: *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Failed to set compute_device_types_mask E5RT: Cannot provide zero compute device types. (1)' Last Exception Backtrace: 0 CoreFoundation 0x0000000199466418 __exceptionPreprocess + 164 1 libobjc.A.dylib 0x00000001967cde88 objc_exception_throw + 76 2 CoreFoundation 0x0000000199560794 -[NSException initWithCoder:] 3 CoreML 0x00000001b4fcfa8c -[MLE5ProgramLibraryOnDeviceAOTCompilationImpl createProgramLibraryHandleWithRespecialization:error:] + 1584 4 CoreML 0x00000001b4fcf3cc -[MLE5ProgramLibrary _programLibraryHandleWithForceRespecialization:error:] + 96 5 CoreML 0x00000001b4fc23d8 __44-[MLE5ProgramLibrary prepareAndReturnError:]_block_invoke + 60 6 libdispatch.dylib 0x00000001a12e1160 _dispatch_client_callout + 20 7 libdispatch.dylib 0x00000001a12f07b8 _dispatch_lane_barrier_sync_invoke_and_complete + 56 8 CoreML 0x00000001b4fc3e98 -[MLE5ProgramLibrary prepareAndReturnError:] + 220 9 CoreML 0x00000001b4fc3bc0 -[MLE5Engine initWithContainer:configuration:error:] + 220 10 CoreML 0x00000001b4fc3888 +[MLE5Engine loadModelFromCompiledArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 344 11 CoreML 0x00000001b4faf53c +[MLLoader _loadModelWithClass:fromArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 364 12 CoreML 0x00000001b4faedd4 +[MLLoader _loadModelFromArchive:configuration:modelVersion:compilerVersion:loaderEvent:useUpdatableModelLoaders:loadingClasses:error:] + 540 13 CoreML 0x00000001b4f9b900 +[MLLoader _loadWithModelLoaderFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 424 14 CoreML 0x00000001b4faaeac +[MLLoader _loadModelFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 460 15 CoreML 0x00000001b4fb0428 +[MLLoader _loadModelFromAssetAtURL:configuration:loaderEvent:error:] + 240 16 CoreML 0x00000001b4fb00c4 +[MLLoader loadModelFromAssetAtURL:configuration:error:] + 104 17 CoreML 0x00000001b5314118 -[MLModelAssetResourceFactoryOnDiskImpl modelWithConfiguration:error:] + 116 18 CoreML 0x00000001b5418cc0 __60-[MLModelAssetResourceFactory modelWithConfiguration:error:]_block_invoke + 72 19 libdispatch.dylib 0x00000001a12e1160 _dispatch_client_callout + 20 20 libdispatch.dylib 0x00000001a12f07b8 _dispatch_lane_barrier_sync_invoke_and_complete + 56 21 CoreML 0x00000001b5418b94 -[MLModelAssetResourceFactory modelWithConfiguration:error:] + 276 22 CoreML 0x00000001b542919c -[MLModelAssetModelVendor modelWithConfiguration:error:] + 152 23 CoreML 0x00000001b5380ce4 -[MLModelAsset modelWithConfiguration:error:] + 112 24 CoreML 0x00000001b4fb0b3c +[MLModel modelWithContentsOfURL:configuration:error:] + 168
2
0
989
Jun ’24
launching a file dialog(choose file dialog window) is crashing in macOS Sonoma 14.2.1
I am unable to select a file where the file dialog window is not coming up to choose file. find the below error from the logs 11:30:00.608 [AWT-EventQueue-0] INFO com.bulloch.scheduler.util.FileChooser - Choose file 2024-01-12 11:30:00.953 java[1716:171065] Suppressing invocation of -[NSApplication runModalForWindow:]. -[NSApplication runModalForWindow:] cannot run inside a transaction begin/commit pair, or inside a transaction commit. Consider switching to an asynchronous equivalent. ( 0 AppKit 0x000000018aeac704 -[NSApplication runModalForWindow:] + 332 1 AppKit 0x000000018b8b45d0 -[NSSavePanel runModal] + 340 2 AppKit 0x000000018b8bcf2c -[NSSavePanel(Deprecated) runModalForDirectory:file:types:] + 200 3 libawt_lwawt.dylib 0x00000001221cd064 -[CFileDialog safeSaveOrLoad] + 340 4 Foundation 0x00000001885c5298 __NSThreadPerformPerform + 264 5 CoreFoundation 0x000000018749fa4c CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28 6 CoreFoundation 0x000000018749f9e0 __CFRunLoopDoSource0 + 176 7 CoreFoundation 0x000000018749f750 __CFRunLoopDoSources0 + 244 8 CoreFoundation 0x000000018749e340 __CFRunLoopRun + 828 9 CoreFoundation 0x000000018749d9ac CFRunLoopRunSpecific + 608 10 Foundation 0x00000001885a7a38 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 212 11 libawt_lwawt.dylib 0x000000012220ef8c Java_sun_lwawt_macosx_LWCToolkit_doAWTRunLoopImpl + 340 12 ??? 0x00000001058e5144 0x0 + 4388180292 13 ??? 0x00000001055bf554 0x0 + 4384879956 14 ??? 0x0000000104e76304 0x0 + 4377240324 15 libjvm.dylib 0x0000000104365f7c _ZN9JavaCalls11call_helperEP9JavaValueP12methodHandleP17JavaCallArgumentsP6Thread + 1892 16 libjvm.dylib 0x000000010439b564 _ZL17jni_invoke_staticP7JNIEnv_P9JavaValueP8_jobject11JNICallTypeP10_jmethodIDP18JNI_ArgumentPusherP6Thread + 644 17 libjvm.dylib 0x000000010439bec8 jni_CallStaticObjectMethod + 432 18 libawt_lwawt.dylib 0x00000001221f1a50 -[CommonComponentAccessibility accessibilityFocusedUIElement] + 636 19 libawt_lwawt.dylib 0x00000001221b1fb0 -[AWTView accessibilityFocusedUIElement] + 156 20 AppKit 0x000000018b7310b0 -[NSWindow(NSWindowAccessibility) accessibilityFocusedUIElement] + 88 21 AppKit 0x000000018ad4c770 NSAccessibilityHandleFocusChangedForce + 136 22 AppKit 0x000000018ad755e0 -[NSWindow _changeKeyAndMainLimitedOK:] + 616 23 AppKit 0x000000018b71f27c -[NSWindow _orderOut:calculatingKeyWithOptions:documentWindow:] + 912 24 AppKit 0x000000018acbc714 NSPerformVisuallyAtomicChange + 108 25 AppKit 0x000000018b720ae0 -[NSWindow _reallyDoOrderWindowOutRelativeTo:] + 448 26 AppKit 0x000000018b720e98 -[NSWindow _reallyDoOrderWindow:] + 80 27 AppKit 0x000000018b7210e8 -[NSWindow _doOrderWindow:] + 264 28 Foundation 0x00000001885c5298 __NSThreadPerformPerform + 264 29 CoreFoundation 0x000000018749fa4c CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28 30 CoreFoundation 0x000000018749f9e0 __CFRunLoopDoSource0 + 176 31 CoreFoundation 0x000000018749f750 __CFRunLoopDoSources0 + 244 32 CoreFoundation 0x000000018749e340 __CFRunLoopRun + 828 33 CoreFoundation 0x000000018749d9ac CFRunLoopRunSpecific + 608 34 HIToolbox 0x0000000191a4c448 RunCurrentEventLoopInMode + 292 35 HIToolbox 0x0000000191a4c0d8 ReceiveNextEventCommon + 220 36 HIToolbox 0x0000000191a4bfdc _BlockUntilNextEventMatchingListInModeWithFilter + 76 37 AppKit 0x000000018ac7a8a4 _DPSNextEvent + 660 38 AppKit 0x000000018b454980 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 716 39 libosxapp.dylib 0x000000011f78425c -[NSApplicationAWT nextEventMatchingMask:untilDate:inMode:dequeue:] + 144 40 AppKit 0x000000018ac6dd50 -[NSApplication run] + 476 41 libosxapp.dylib 0x000000011f784038 +[NSApplicationAWT runAWTLoopWithApp:] + 184 42 libawt_lwawt.dylib 0x000000012220e30c +[AWTStarter starter:headless:] + 500 43 libosxapp.dylib 0x000000011f785c20 +[ThreadUtilities invokeBlockCopy:] + 28 44 Foundation 0x00000001885c5298 __NSThreadPerformPerform + 264 45 CoreFoundation 0x000000018749fa4c CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 28 46 CoreFoundation 0x000000018749f9e0 __CFRunLoopDoSource0 + 176 47 CoreFoundation 0x000000018749f750 __CFRunLoopDoSources0 + 244 48 CoreFoundation 0x000000018749e340 __CFRunLoopRun + 828 49 CoreFoundation 0x000000018749d9ac CFRunLoopRunSpecific + 608 50 java 0x0000000102f2022c CreateExecutionEnvironment + 824 51 java 0x0000000102f1caf8 JLI_Launch + 1576 52 java 0x0000000102f22a38 main + 76 53 dyld 0x00000001870410e0 start + 2360 ) 11:30:00.958 [AWT-EventQueue-0] WARN com.bulloch.scheduler.sched.ImportFileHelper - No file selected to import
1
0
702
Jan ’24