问题描述
我有这种方法:
+ (MHTwitterParser*)createParser:(NSString*)format {
if ([format compare:@"json"] == NSOrderedSame) {
return [[MHJsonTwitterParser alloc] init];
}
[NSException raise:@"Unknown format" format:@"Unknown format of parser"];
}
编译器抱怨:
Control may reach end of non-void function
这只是一个警告,但这并不重要。
It is just a warning, but it does not matter.
最明显的解决方法是在 [NSException之后添加
。 return nil;
提高:...
Obvious fix for that is to add for example return nil;
after the [NSException raise: ...
.
但是,我认为这是不必要的(甚至会误导读者),因为抛出了异常,因此控制可能会到达非空函数的末尾并不是正确的。还是我想念某些东西...?仅仅是编译器不完善还是有一定的原因?
However, I think it is not needed (and is even misleading for readers), because the exception is thrown, so it is not true that "Control may reach end of non-void function". Or am I missing something ...? Is it only compiler imperfection or there is some considerable reason for this?
编译器是Apple LLVM编译器3.1
The compiler is Apple LLVM compiler 3.1
推荐答案
原因很简单。
对于编译器,方法 [NSException提高:... ]
是黑盒方法。它不知道该方法实际上会引发异常。
For the compiler, the method [NSException raise: ...]
is a black box method. It doesn't know that the method will actually raise an exception.
如果将其与Java或C ++进行比较,它们的会抛出
语句是一种语言功能,编译器在找到它时会确切知道会发生什么。在Obj-C中,情况有所不同,有时取决于运行时条件。请考虑以下内容。
If you compare it with Java or C++, their throw
statements are a language feature and the compiler knows exactly what will happen when it finds it. In Obj-C it's different and sometimes it depends on runtime conditions. Consider the following.
NSException* exception = nil;
if (someCondition) {
exception = [NSException exceptionWithName:...];
}
[exception raise];
编译器不会知道是否确实引发了异常。
The compiler won't know if the exception is really raised or not.
这篇关于NSException raise:format:作为方法中的最后一条语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!