---
title: Record handled exception
source: https://docs.newrelic.com/docs/mobile-monitoring/new-relic-mobile/mobile-sdk/record-handled-exceptions
---

## Android

### Syntax [#android-syntax]

#### Java [#android-syntax-java]

```java
NewRelic.recordHandledException(Exception $exceptionToHandle)
NewRelic.recordHandledException(Exception $exceptionToHandle, Map of String, Object $exceptionAttributes)
```

```java
NewRelic.recordHandledException(Throwable $throwableToHandle)
NewRelic.recordHandledException(Throwable $throwableToHandle, Map of String, Object $exceptionAttributes)
```

#### Kotlin [#android-syntax-kotlin]

```kotlin
NewRelic.recordHandledException(
  exception: Exception?,
  exceptionAttributes: Map<String?, Any?>?
)
```

```kotlin
NewRelic.recordHandledException(throwable: Throwable?)
NewRelic. recordHandledException(
  throwable: Throwable?,
  attributes: Map<String?, Any?>?
)
```

### Description [#android-description]

Records a handled exception or other throwable type. Optionally takes map with additional attributes showing context.

Use `recordHandledException()` within a `try{...} catch(){...}` block to help understand how often your application is throwing exceptions, and under what conditions.

In addition to associated custom attributes, the events will also have associated [session attributes](https://docs.newrelic.com/docs/insights/insights-data-sources/default-attributes/mobile-default-attributes-insights). You can view event data in the mobile monitoring UI in the [Crash event trail](https://docs.newrelic.com/docs/mobile-monitoring/mobile-monitoring-ui/crashes/mobile-crash-event-trail), or via NRQL.

### Parameters [#android-parameters]

| Parameter              | Type                    | Description                                                        |
| ---------------------- | ----------------------- | ------------------------------------------------------------------ |
| `$exceptionToHandle`   | `Exception`             | Required. The exception to be recorded.                            |
| `$exceptionAttributes` | `Map of String, Object` | Optional. A map of attributes to be associated with the exception. |

### Return values [#android-return-values]

Returns `true` if recorded successfully, or `false` if not.

### Examples [#android-examples]

Here's an example of recording a `ClassCastException` from within an on-click listener:

#### Java [#android-examples-java]

```java
public class MainActivity extends Activity {
  ... coolButton.setOnClickListener(new View.OnClickListener() {
    Map myMap = new HashMap<>();
    @Override
    public void onClick(View view) {
      try {
        myMap.put("Key", "Value");
        Integer stringVar =
            (Integer) myMap.get("Key"); // throws ClassCastException
      } catch(Exception e) {
        NewRelic.recordHandledException(e, myMap);
      }
    }
  });
  ...
}
```

#### Kotlin [#android-examples-kotlin]

```kotlin
class MainActivity : AppCompatActivity() {
  ...
  binding.fab.setOnClickListener { view ->
    val myMap = mutableMapOf<String,Any>()
    try {
      myMap["Key"] = "Value"
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
          .setAnchorView(R.id.fab)
          .setAction("Action", null).show()
    } catch(exception:Exception) {
      NewRelic.recordHandledException(exception,myMap)
    }
  }
  ...
}
```

## iOS

### Syntax [#ios-syntax]

#### Objective-c

```objectivec
recordHandledException:(NSException* __nonnull)exception withAttributes:(NSDictionary* __nullable)attributes;
```

### Description [#ios-description]

Records a handled exception (Objective-c only). Optionally takes a map with additional attributes showing context.

The `recordHandledException` API is useful for crash analysis; the captured events will help you understand how often your application is throwing exceptions and under what conditions. In addition to associated custom attributes, the events will also have associated [session attributes](https://docs.newrelic.com/docs/insights/insights-data-sources/default-attributes/mobile-default-attributes-insights).

This API takes an instance of an `NSException` and an optional `NSDictionary` attribute dictionary, then creates a `recordHandledException` event. You can view event data in the [Crash event trail UI](https://docs.newrelic.com/docs/mobile-monitoring/mobile-monitoring-ui/crashes/mobile-crash-event-trail), and query them with NRQL.

> #### ⚠️ IMPORTANT
>
> This function should not be used with Swift code. Please use [`recordError`](https://docs.newrelic.com/docs/mobile-monitoring/new-relic-mobile/mobile-sdk/record-errors/) to track handled errors in Swift code.

### Parameters [#ios-parameters]

| Parameter     | Type           | Description                                           |
| ------------- | -------------- | ----------------------------------------------------- |
| `exception`   | `NSException`  | Required. The exception to be recorded.               |
| `attributes​` | `NSDictionary` | Optional. Dictionary of attributes that give context. |

### Return values [#ios-return-values]

Returns `true` if the event is recorded successfully, or `false` if not.

### Examples [#ios-examples]

Here's an example of a simple handled exception:

```objectivec
try {
  @throw [NSException exceptionWithName:@"versionException"
                                 reason:@"App version no longer supported"
                               userInfo:nil];
} @catch(NSException* e) {
  [NewRelic recordHandledException:e];
}
```

Here's another example or a handled exception with a dictionary:

```objectivec
NSException* exception = [NSException exceptionWithName:@"MyException"
                                                 reason:@"I have my reason"
                                               userInfo:nil];

NSDictionary* dictionary = @{@"int" : @1, @"Test Group" : @"A | B"};

[NewRelic recordHandledException:exception withAttributes:dictionary];
```

## .NET MAUI

### Syntax [#maui-syntax]

```csharp
RecordException(System.Exception exception) : void;
```

### Description [#maui-description]

Manually record non-fatal exceptions.

### Parameters [#maui-parameters]

| Parameter   | Type        | Description                             |
| ----------- | ----------- | --------------------------------------- |
| `exception` | `Exception` | Required. The exception to be recorded. |

### Return values [#maui-return-values]

Returns `true` if the event is recorded successfully, or `false` if not.

### Example [#maui-example]

```csharp
    try {
      some_code_that_throws_error();
    } catch (Exception ex) {
      CrossNewRelic.Current.RecordException(ex);
    }
```

## Unity

### Syntax [#unity-syntax]

```csharp
RecordException(System.Exception exception) : void;
```

### Description [#unity-description]

Records a handled exception. Optionally takes map with additional attributes showing context.

### Parameters [#unity-parameters]

| Parameter   | Type        | Description                             |
| ----------- | ----------- | --------------------------------------- |
| `exception` | `Exception` | Required. The exception to be recorded. |

### Return values [#unity-return-values]

Returns `true` if the event is recorded successfully, or `false` if not.

### Example [#unity-example]

```csharp
try {
  some_code_that_throws_error();
} catch(Exception ex) {
  NewRelicAgent.RecordException(e);
}
```
