Exception Handling - Objective C

The Objective-C language has an exception-handling syntax similar to that of Java and C++. By using this syntax with the NSException, NSError, or custom classes, you can add robust error-handling to your programs. This post provides a summary of exception syntax and handling, presenting dubug information; for more details, see Exception Programming Topics.

Exception Handling : An exception is a special condition that interrups the normal flow of program execution.
The class that handles exception in objective c is NSException, creating an exception object and throwing it at runtime is called throwing an exception.

Objective-C exception support involves four compiler directives
  1. @try:     Code that can potentially throw an exception is enclosed in a @try{} block.
  2. @catch: @catch{} block contains exception-handling logic for exception thrown in a @try
  3. @throw: USe the @throw directive to throw an exception, which is an Objective-C object.
  4. @finally: @finally{} block contains code that must be executed whether an exception is thrown or not.
Note: You can have multiple @catch{} blocks to catch different types of exception. For a code example, see Catching Different Types of Exceptions.

NSException class: NSException is used to implement exception handling and contains information about an exception.
With the help of the methods defined in the NSException class you can not only handle the exceptions but also come to know about the exception name and the reason why this exception has occurred.

Example:  Now let’s have a look at a small demo of exception handling where I am trying to call an instance method of a class which is not defined in the class so of course it will throw an exception so I have handled the exception using the @try and @catch block. Given below are the snap shot of the code displaying the @try and @catch block.

Exception Text
Exception Text

Message in the Console:

Console Message
Console Message

When the exception occurs, the @catch block gets executed. An NSException object that contains information about the exception gets passed as the argument into this block. As you can see, the name method retrieves the name of the exception, and the reason method gives the reason.

Throwing an Exception: Try to handle the error, or use something like NSError or the NSAssert method.

Throwing exceptions is a rather “expensive” procedure, and of course uncaught exceptions will cause your program to crash. That being said, throwing an exception is straightforward:

NSException *theException = [NSException exceptionWithName:@"File Not Found" reason:@"No such file" userInfo:nil];

@throw theException;

Conclusion: Exceptions provide a powerful and flexible way to handle issues in your code. Use them wisely—don’t make the runtime system a massive juggling round.

I hope that you have understood exception handling in objective c in case if you are having any queries then feel free to mail us at  pardeepkchaudhary@aalizwel.com or you can mention your queries via comments.

Follow our page on Facebook.

Popular posts from this blog

Play youtube video in iOS app

Disabling print for Production In Swift Project