---
title: "Mechanisms for Handling Exception in Objective-C"  
description: "Whenever we create any program or application it may be chances to occurring errors or exceptions at runtime and our application stops working or hang"  
author: "Tarun Kumar"  
published: 2015-10-26  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10969/mechanisms-for-handling-exception-in-objective-c  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Mechanisms for Handling Exception in Objective-C

Whenever we create any program or [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) it may be chances to occurring errors or [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) at runtime and our application [stops working](https://answers.mindstick.com/qa/50437/how-to-fix-messenger-that-keeps-crashing-or-stops-working-on-your-iphone-x-after-installing-a-new-ios-update) or hanging problem with application, at that moment when we face that type of problems it looks ugly according to the user, So dealing these types of exceptional conditions [Foundation](https://www.mindstick.com/articles/157275/passed-peoplecert-it-infrastructure-library-itil-4-foundation-exam-today) [Framework](https://www.mindstick.com/forum/34615/software-framework-vs-library) of Objective-C provides exception [handling mechanisms](https://www.mindstick.com/forum/160175/describe-rust-s-error-handling-mechanisms-including-the-result-and-option-types) for our program/application. As a result, our code can be much cleaner, easier to write correctly and also easier to maintain.

[Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) is based on four compiler [directives](https://www.mindstick.com/blog/44/directives-in-asp-dot-net):

\

- @try : Here we define a block of code that can potentially [throw an exception](https://www.mindstick.com/forum/159351/how-does-a-java-method-throw-an-exception-before-it-s-called), it is also called exception handling domain.

- @catch() : Here we define a block of code that is thrown after the exception caught by @try block. The parameter of @catch is the exception object thrown locally, this is usually an NSException object, but can be other types of objects, such as NSString objects.

- @finally : Here we define a block of code that is subsequently executed whether an exception is thrown or not.

- @throw : This directive is used to throw the exception objects. We throw an exception by instantiating an NSException object and then doing one of two things with it:

Using it as the argument of a @throw compiler directive Sending it a raise message

Following example shows how we throw an exception using the @throw directive:

NSException *myObj = [NSException exceptionWithName:@”File Not Found Exception”

```
 Reason:@”FileNotFound on System”  userInfo:nil];     @throw myObj;    // [myObj  raise];  /* equivalent to above directive */
```

---

Original Source: https://www.mindstick.com/blog/10969/mechanisms-for-handling-exception-in-objective-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
