ios – Objective C – 从块内部引发的Catch异常

ios – Objective C – 从块内部引发的Catch异常,第1张

概述我在app中使用以下代码: @try { if(!self.usernameField.text || [self.usernameField.text isEqualToString:@""]) [NSException raise:@"Invalid value for username" format:@"Please enter your usern 我在app中使用以下代码:

@try {        if(!self.usernameFIEld.text || [self.usernameFIEld.text isEqualToString:@""])            [NSException raise:@"InvalID value for username" format:@"Please enter your username."];        if(!self.passwordFIEld.text || [self.passwordFIEld.text isEqualToString:@""])            [NSException raise:@"InvalID value for password" format:@"Please enter your password."];        [LoginManager         userLogin:self.usernameFIEld.text         andPassword:self.passwordFIEld.text         success:^(AFhttpRequestoperation *op,ID response) {             if([self.delegate respondsToSelector:@selector(loginSuccessWithUsername:)]) {                 [self.delegate performSelector:@selector(loginSuccessWithUsername:)withObject:self.usernameFIEld.text];             }             [self dismisspopoverController];         }         failure:^(AFhttpRequestoperation *op,NSError *err) {             Nsstring* nsLocalizedRecoverySuggestion = [err.userInfo objectForKey:@"NSLocalizedRecoverySuggestion"];             if(err.code == -1009) {                 [NSException raise:@"No Internet connection" format:@"It appears you’re not connected to the internet,please configure connectivity."];             }             if([nsLocalizedRecoverySuggestion rangeOfString:@"Wrong username or password."].location != NSNotFound) {                 [NSException raise:@"InvalID username or password" format:@"Your given username or password is incorrect"];             }             else {                 [NSException raise:@"BSXLoginVIEwController" format:@"Error during login"];             }         }];    }    @catch (NSException *exception) {        UIAlertVIEw* alert = [[UIAlertVIEw alloc]initWithTitle:@"Login error"                                                       message:exception.description                                                      delegate:self                                             cancelbuttonTitle:@"Ok"                                             otherbuttonTitles:nil];        [alert show];    }

但是,在故障块中引发异常不会在catch部分中被捕获.
我有点理解为什么它是合乎逻辑的,但是我想知道是否有办法告诉块我内部发生的异常应该由我创建的catch部分来处理.

谢谢你的帮助!

此致
佐利

解决方法 不要这样做.首先,我确信你至少会得到@bbum关于这一点的评论,NSException不是在Objective-C中用于可恢复的错误,而是通过代码传播可恢复的错误(参见 Introduction to Exception Programming Topics for Cocoa).相反,Objective-C中使用的构造基本上是针对不可恢复的编程错误使用NSException,并使用NSError对象来处理可恢复的错误.

但是,你在这里遇到了一个更大的问题,你正在进行的调用会阻止回调,因为它们会在完成之前返回.在这种情况下,在实际抛出异常之前很久就会退出异常处理程序.

在这种情况下,我建议删除异常并处理实际故障中的错误:通过调度到主队列并在那里呈现UIAlert来阻止.

总结

以上是内存溢出为你收集整理的ios – Objective C – 从块内部引发的Catch异常全部内容,希望文章能够帮你解决ios – Objective C – 从块内部引发的Catch异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1017698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存