使用@try处理异常

作者:追风剑情 发布于:2019-2-22 16:39 分类:Objective-C

示例

  1. #import <Foundation/Foundation.h>
  2. #import "Fraction.h"
  3.  
  4. int main(int argc, const char * argv[]) {
  5. @autoreleasepool {
  6. //主动抛出异常
  7. //@throw [NSException exceptionWithName:@"context show" reason:@"crush's reason" userInfo:nil];
  8. Fraction *f = [[Fraction alloc] init];
  9.  
  10. @try {
  11. [f noSuchMethod];
  12. }
  13. @catch (NSException *exception) {
  14. NSLog(@"Caught %@%@", [exception name], [exception reason]);
  15. }
  16. @finally {
  17. NSLog(@"finally...");
  18. }
  19. NSLog(@"Execution continues!");
  20. }
  21. return 0;
  22. }

标签: Objective-C

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号