Objective-C exceptions are similar to Java exception handling, and @ finally processing is also required, regardless of whether the exception is captured or not. Syntax captured by Exception Handling
@try {
<#statements#>
}
@catch (NSException *exception) {
<#handler#>
}
@finally {
<#statements#>
}
The @ catch {} block should capture exceptions in detail, that is, capture specific exceptions first, and then use some generic exception types. We can customize two exception classes to see how to handle exceptions. 1. New SomethingException and SomeOverException classes are inherited from the 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. Create a Box class with exceptions under certain conditions.
#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 100"
userInfo: nil];
@throw e;
} else if ( number >= 6 ) {
// throw warning
NSException *e = [SomethingException
exceptionWithName: @"BoxWarningException"
reason: @"The level is above or at 60"
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 of The number is 0. You can use the pushIn method to push the number into the Box. After each call, the number is added to 1. somethingException is generated when the number is greater than or equal to 6, indicating that the number reaches or exceeds 6. If the number exceeds 10, SomeOverException is generated. If the number is less than 1, a normal NSException exception is generated.
Enter The name and reason for The [SomeOverException exceptionWithName: @ "BoxOverflowException" reason: @ "The level is abve 100" exception, which can be obtained during capture.
3. Use Box to add and catch exception 3.1 of Box class as appropriate. If no more than 6, no exception exists.
- (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];
}
}
Print result:
Box number is: 1
Box number is: 2
Box number is: 3
Box number is: 4
Box number is: 5
An exception occurs when the value of 3.2 exceeds 6.
for (int i = 0; i < 11; i++) {
[box pushIn];
[box printNumber];
}
09:12:05. 889 ObjectiveCTest [648: f803] Box number is: 1
09:12:05. 890 ObjectiveCTest [648: f803] Box number is: 2
09:12:05. 890 ObjectiveCTest [648: f803] Box number is: 3
09:12:05. 890 ObjectiveCTest [648: f803] Box number is: 4
09:12:05. 891 ObjectiveCTest [648: f803] Box number is: 5
09:12:05. 891 ObjectiveCTest [648: f803] *** Terminating app due to uncaught exception 'boxwarningexception', reason: 'The number is above or 60'
This is when the program throws an exception and crashes. So how can we avoid program crash and handle exceptions. 3.3 Add Exception Handling
for (int i = 0; i < 11; 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, print the result:
09:14:35. 165 ObjectiveCTest [688: f803] Box number is: 1
09:14:35. 167 ObjectiveCTest [688: f803] Box number is: 2
09:14:35. 167 ObjectiveCTest [688: f803] Box number is: 3
09:14:35. 167 ObjectiveCTest [688: f803] Box number is: 4
09:14:35. 167 ObjectiveCTest [688: f803] Box number is: 5
09:14:35. 167 ObjectiveCTest [688: f803] BoxWarningException The number is abve or at 60
09:14:35. 168 ObjectiveCTest [688: f803] Box number is: 6
09:14:35. 168 ObjectiveCTest [688: f803] BoxWarningException The number is abve or at 60
09:14:35. 168 ObjectiveCTest [688: f803] Box number is: 7
09:14:35. 168 ObjectiveCTest [688: f803] BoxWarningException The number is abve or at 60
09:14:35. 168 ObjectiveCTest [688: f803] Box number is: 8
09:14:35. 168 ObjectiveCTest [688: f803] BoxWarningException The number is abve or at 60
09:14:35. 169 ObjectiveCTest [688: f803] Box number is: 9
09:14:35. 169 ObjectiveCTest [688: f803] BoxWarningException The number is abve or at 60
09:14:35. 169 ObjectiveCTest [688: f803] Box number is: 10
09:14:35. 169 ObjectiveCTest [688: f803] BoxOverflowException
09:14:35. 225 ObjectiveCTest [688: f803] Box number is: 11
SomeOverException is thrown when the value exceeds 10. 3.4. If the value is less than 0, the exception is in the setNumber of the Box class. If the value is smaller than 0, a common exception is thrown.
@try {
[box setNumber:-10];
}
@catch (NSException *exception) {
NSLog(@"%@",[exception name]);
}
@finally {
[box printNumber];
}
Print results
09:17:42. 405 ObjectiveCTest [753: f803] BoxUnderflowException
09:17:42. 406 ObjectiveCTest [753: f803] Box number is:-10