- Basic Objective-C
- Objective-C - Home
- Objective-C - Overview
- Objective-C - Environment Setup
- Objective-C - Program Structure
- Objective-C - Basic Syntax
- Objective-C - Data Types
- Objective-C - Variables
- Objective-C - Constants
- Objective-C - Operators
- Objective-C - Loops
- Objective-C - Decision Making
- Objective-C - Functions
- Objective-C - Blocks
- Objective-C - Numbers
- Objective-C - Arrays
- Objective-C - Pointers
- Objective-C - Strings
- Objective-C - Structures
- Objective-C - Preprocessors
- Objective-C - Typedef
- Objective-C - Type Casting
- Objective-C - Log Handling
- Objective-C - Error Handling
- Command-Line Arguments
- Advanced Objective-C
- Objective-C - Classes & Objects
- Objective-C - Inheritance
- Objective-C - Polymorphism
- Objective-C - Data Encapsulation
- Objective-C - Categories
- Objective-C - Posing
- Objective-C - Extensions
- Objective-C - Protocols
- Objective-C - Dynamic Binding
- Objective-C - Composite Objects
- Obj-C - Foundation Framework
- Objective-C - Fast Enumeration
- Obj-C - Memory Management
- Objective-C Useful Resources
- Objective-C - Quick Guide
- Objective-C - Useful Resources
- Objective-C - Discussion
Exception handling in Objective-C
Exception handling is made available in Objective-C with foundation class NSException.
Exception handling is implemented with the following blocks −
@try − This block tries to execute a set of statements.
@catch − This block tries to catch the exception in try block.
@finally − This block contains set of statements that always execute.
#import <Foundation/Foundation.h> int main() { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSMutableArray *array = [[NSMutableArray alloc]init]; @try { NSString *string = [array objectAtIndex:10]; } @catch (NSException *exception) { NSLog(@"%@ ",exception.name); NSLog(@"Reason: %@ ",exception.reason); } @finally { NSLog(@"@@finaly Always Executes"); } [pool drain]; return 0; }
2013-09-29 14:36:05.547 Answers[809:303] NSRangeException 2013-09-29 14:36:05.548 Answers[809:303] Reason: *** -[__NSArrayM objectAtIndex:]: index 10 beyond bounds for empty array 2013-09-29 14:36:05.548 Answers[809:303] @finally Always Executes
In the above program, instead of the program terminating due to the exception, it continues with the subsequent program since we have used exception handling.
objective_c_foundation_framework.htm
Advertisements
To Continue Learning Please Login
Login with Google