----exception Handling for iOS development

Source: Internet
Author: User
Tags finally block throw exception

This article reprinted to http://blog.csdn.net/chenyong05314/article/details/7906593

Reprinted from: http://blog.sina.com.cn/s/blog_71715bf8010166qf.html

A big Talk:

The exception handling notation for Object-c languages is similar to C + + and Java. Plus with Nsexception,nserror or custom classes, you can add a powerful error handling mechanism to your application. The exception handling mechanism is supported by this four keyword: @try, @catch, @thorw, @finally. When the code is likely to have an exception, we put him in the @try statement block. The @catch () block contains the logic to handle the thrown exception in the @try block. The statement inside @finally Block executes regardless of whether the exception occurred. If you use the @throw block directly to throw an exception, the exception is essentially an OC object. We can use NSException objects, but they are not limited to them.

OBJECTIVE-C exceptions are compared to Java exception handling, as well as @finally processing, regardless of whether the exception is captured or not.

Syntax for exception handling capture:

    • @Try {
    • < #statements #>
    • }
    • @catch (NSException *exception) {
    • < #handler #>
    • }
    • @finally {
    • < #statements #>
    • }

@catch {} block the catch of the exception should be thin and thick, that is, catch a specific exception before using some of the generic exception types.

We customize two exception classes to see the use of exception handling.

1, the new somethingexception,someoverexception these two classes, both inherit and NSException class.

SomethingException.h

    • #import <Foundation/Foundation.h>
    • @interface somethingexception:nsexception
    • @end

Somethingexception.m

    • #import "SomethingException.h"
    • @implementation Somethingexception
    • @end

SomeOverException.h

    • #import <Foundation/Foundation.h>
    • @interface someoverexception:nsexception
    • @end

Someoverexception.m

    • #import "SomeOverException.h"
    • @implementation Someoverexception
    • @end

2, New box class, under certain conditions to produce an exception.

    • #import <Foundation/Foundation.h>
    • @interface Box:nsobject
    • {
    • Nsinteger number;
    • }
    • -(void) Setnumber: (Nsinteger) num;
    • -(void) Pushin;
    • -(void) pullout;
    • -(void) Printnumber;
    • @end

  • @implementation Box
  • -(ID) init {
  • self = [super init];
  • if (self) {
  • [Self setnumber:0];
  • }
  • return self;
  • }
  • -(void) Setnumber: (Nsinteger) num {
  • Number = num;
  • if (number > 10) {
  • NSException *e = [someoverexception
  • Exceptionwithname: @"Boxoverflowexception"
  • Reason: @"The level is above"
  • Userinfo:nil];
  • @throw e;
  • } Else if (number >= 6) {
  • Throw warning
  • NSException *e = [somethingexception
  • Exceptionwithname: @"Boxwarningexception"
  • Reason: @"The level was above or at
  • Userinfo:nil];
  • @throw e;
  • } Else if (number < 0) {
  • Throw Exception
  • NSException *e = [nsexception
  • Exceptionwithname: @"Boxunderflowexception"
  • Reason: @"The level is below 0"
  • Userinfo:nil];
  • @throw e;
  • }
  • }
  • -(void) Pushin {
  • [Self Setnumber:number + 1];
  • }
  • -(void) pullout {
  • [Self setnumber:number-1];
  • }
  • -(void) Printnumber {
  • NSLog (@"box number is:%d", number);
  • }
  • @end

The function of this class is that when the box is initialized, the number is 0 and can be pushed into box using the Pushin method, number plus 1 for each call. A somethingexception exception occurs when number is greater than or equal to 6. Tells you that the number reaches or exceeds 6, and more than 10 produces a someoverexception exception, less than 1 produces a common nsexception exception.

write here [someoverexception exceptionwithname:@ "Boxoverflowexception" reason:@ "the level is above 100" exception name and reason that can be obtained when capturing.

3. Use box to capture exceptions to the box class with appropriate additions

3.1, in no more than 6 o'clock, no abnormalities

  • -(void) viewdidload
  • {
  • [Super Viewdidload];
  • NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  • Box *box = [[Box Alloc]init];
  • For (int i = 0; i < 5; i++) {
  • [Box Pushin];
  • [Box Printnumber];
  • }
  • }

Printing results:

Box number Is:1

Box number Is:2

Box number Is:3

Box number Is:4

Box number Is:5

3.2 More than 6, producing an exception

    • for (int i = 0; i < i++) {
    • [Box Pushin];
    • [Box Printnumber];
    • }

  • 2012-07-04 09:12:05.889 objectivectest[648:f803] Box number is:1
  • 2012-07-04 09:12:05.890 objectivectest[648:f803] Box number Is:2
  • 2012-07-04 09:12:05.890 objectivectest[648:f803] Box number Is:3
  • 2012-07-04 09:12:05.890 objectivectest[648:f803] Box number is:4
  • 2012-07-04 09:12:05.891 objectivectest[648:f803] Box number Is:5
  • 2012-07-04 09:12:05.891 objectivectest[648:f803] * * * terminating app due to uncaught exception ' boxwarningexception ' , Reason: ' The number is above or at

This is when the program throws an abnormal crash. How to make the program does not crash, do exception handling.

3.3. Plus exception handling

  • for (int i = 0; i < i++) {
  • @Try {
  • [Box Pushin];
  • }
  • @catch (Somethingexception *exception) {
  • NSLog (@"%@%@", [exception name], [exception reason]);
  • }
  • @catch (Someoverexception *exception) {
  • NSLog (@"%@", [exception name]);
  • }
  • @finally {
  • [Box Printnumber];
  • }
  • }

Run, the program does not crash, printing results:

  • 2012-07-04 09:14:35.165 objectivectest[688:f803] Box number is:1
  • 2012-07-04 09:14:35.167 objectivectest[688:f803] Box number Is:2
  • 2012-07-04 09:14:35.167 objectivectest[688:f803] Box number Is:3
  • 2012-07-04 09:14:35.167 objectivectest[688:f803] Box number is:4
  • 2012-07-04 09:14:35.167 objectivectest[688:f803] Box number Is:5
  • 2012-07-04 09:14:35.167 objectivectest[688:f803] boxwarningexception The number is above or at 60
  • 2012-07-04 09:14:35.168 objectivectest[688:f803] Box number Is:6
  • 2012-07-04 09:14:35.168 objectivectest[688:f803] boxwarningexception The number is above or at 60
  • 2012-07-04 09:14:35.168 objectivectest[688:f803] Box number is:7
  • 2012-07-04 09:14:35.168 objectivectest[688:f803] boxwarningexception The number is above or at 60
  • 2012-07-04 09:14:35.168 objectivectest[688:f803] Box number Is:8
  • 2012-07-04 09:14:35.168 objectivectest[688:f803] boxwarningexception The number is above or at 60
  • 2012-07-04 09:14:35.169 objectivectest[688:f803] Box number Is:9
  • 2012-07-04 09:14:35.169 objectivectest[688:f803] boxwarningexception The number is above or at 60
  • 2012-07-04 09:14:35.169 objectivectest[688:f803] Box number is:10
  • 2012-07-04 09:14:35.169 objectivectest[688:f803] Boxoverflowexception
  • 2012-07-04 09:14:35.225 objectivectest[688:f803] Box number is:11

Over 10 o'clock, the someoverexception exception is thrown.

3.4, less than 0 o'clock of the exception

In the box class Setnumber, when number is less than 0 o'clock, we throw a common exception.

    • @Try {
    • [Box setnumber:-10];
    • }
    • @catch (NSException *exception) {
    • NSLog (@"%@", [exception name]);
    • }
    • @finally {
    • [Box Printnumber];
    • }

Printing results:

    • 2012-07-04 09:17:42.405 objectivectest[753:f803]  boxunderflowexception  
    • 2012-07-04 09:17:42.406 objectivectest[753: F803] box number is: -10  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.