什么是NSException?在開發(fā)的過程中其實經(jīng)常用到NSException,但是很多程序員卻不清楚NSException是什么。控制臺輸出的日志信息就是NSException產(chǎn)生的,一旦程序拋出異常,程序就會崩潰,控制臺就會有這些崩潰日志,來讓程序員意識到自己代碼哪里有問題。
NSException的基本用法
//異常的名稱
NSString*exceptionName =@"自定義異常";
//異常的原因
NSString*exceptionReason =@"自定義異常原因";
//異常的信息
NSDictionary*exceptionUserInfo =nil;
NSException*exception = [NSExceptionexceptionWithName:exceptionName reason:exceptionReason userInfo:exceptionUserInfo];
NSString*aboutMe =@"我要崩潰";
if([aboutMe isEqualToString:@"我要崩潰"]) {
//拋異常 這邊就能如愿崩潰了
@throwexception;? ?
?}
//獲取崩潰具體信息
NSDictionary *dict = @{@"類名": [NSString stringWithFormat:@"%@",[self class]],@“哪一行":[NSString stringWithFormat:@"%ld",(long)__LINE__],@"方法名":[NSString stringWithFormat:@"%s",__PRETTY_FUNCTION__]};