如何将异常转换为NSError对象 [英] How to convert an exception into an NSError object
问题描述
我想将异常消息转换为NSError对象,以便可以在try-catch块中使用它(我实际上正在研究 React Native的本地iOS模块)。
I want to convert the message of an exception into an NSError object so that I can use it within a try-catch block (I'm actually working on a native iOS module for React Native).
RCT_EXPORT_METHOD(myMethod:(NSDictionary *)dict
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
@try {
// Do something which could throw something (NS Error or NS Exception)
resolve(nil);
} @catch (NSException *exception) {
// HERE I WANT TO TRANSFORM THE EXCEPTION exception INTO AN ERROR error
NSError error = ???
reject(@"my_error", @"Could not do something important", error);
}
}
我想将异常转换为 NSError
,因为 reject
函数的第三个参数(拒绝JS端的Promise)期望输入的类型为 NSError
。在这种情况下,我不确定我的解决方案(使用try-catch)是否是最好的方法。
I want to convert the exception into an NSError
because the third parameter of the reject
function (which rejects a Promise on the JS side) expects the input to be of type NSError
. I'm not sure whether my solution (using try-catch) is the best thing in this scenario..
您可以将异常转换为NSError对象,然后在警报面板中将错误对象中的信息呈现给用户。
You can convert an exception into an NSError object and then present the information in the error object to the user in an alert panel.
但是,指南中没有显示代码示例,而仅显示了第二种方法的代码示例。您还可以在包含错误参数的方法中间接返回它们这对于我想要的东西来说似乎很复杂。
However the guide does not show a code sample for that and only shows a code sample for a second approach You can also return them indirectly in methods that include an error parameter which seems to complicated for what I want.
那么,如何将异常转换为NSError? NSError的API参考似乎不包含合适的功能。
So, how would I convert an exception into an NSError? The API reference of NSError does not seem to contain a suitable function..
推荐答案
您不能将 NSException
转换为 NSError
,因为 NSException
它们没有相同的属性。是否有任何理由要捕获 NSException
而不是构建 ** NSError
机制?异常通常是致命的且不可恢复,而错误则是非致命的且可恢复。
You can't convert NSException
to NSError
because NSException
they do not have the same properties. Is there any reason for catching NSException
instead of building a **NSError
mechanism? Exceptions are usually fatal and non-recoverable while Errors are non-fatal and recoverable.
如果您需要通过转换 NSException 来完成 code>转换为
NSError
,您可以像这样手动进行操作:
If you need to go through with converting an NSException
to an NSError
anyway, you can do it manually like so:
@try {
// Something
} @catch (NSException *exception) {
NSMutableDictionary * info = [NSMutableDictionary dictionary];
[info setValue:exception.name forKey:@"ExceptionName"];
[info setValue:exception.reason forKey:@"ExceptionReason"];
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
NSError *error = [[NSError alloc] initWithDomain:yourdomain code:errorcode userInfo:info];
//use error
}
这篇关于如何将异常转换为NSError对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!