• EnglishEspañol日本語한국어Português
  • Log inStart now

Record handled exception

Syntax

Java

NewRelic.recordHandledException(Exception $exceptionToHandle)
NewRelic.recordHandledException(Exception $exceptionToHandle, Map of String, Object $exceptionAttributes)
NewRelic.recordHandledException(Throwable $throwableToHandle)
NewRelic.recordHandledException(Throwable $throwableToHandle, Map of String, Object $exceptionAttributes)

Kotlin

NewRelic.recordHandledException(
exception: Exception?,
exceptionAttributes: Map<String?, Any?>?
)
NewRelic.recordHandledException(throwable: Throwable?)
NewRelic. recordHandledException(
throwable: Throwable?,
attributes: Map<String?, Any?>?
)

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. You can view event data in the mobile monitoring UI in the Crash event trail, or via NRQL.

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

Returns true if recorded successfully, or false if not.

Examples

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

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

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)
}
}
........

Syntax

Objective-c

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

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.

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, and query them with NRQL.

Important

This function should not be used with Swift code. Please use recordError to track handled errors in Swift code.

Parameters

Parameter

Type

Description

exception

NSException

Required. The exception to be recorded.

attributes​

NSDictionary

Optional. Dictionary of attributes that give context.

Return values

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

Examples

Here's an example of a simple handled exception:

@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:

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];

Syntax

recordError(error, StackTrace.current, attributes: attributes);

Description

Manually record non-fatal exceptions.

Parameters

Parameter

Type

Description

exception

Exception

Required. The exception to be recorded.

Return values

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

Example

try {
some_code_that_throws_error();
} catch (ex) {
NewrelicMobile.instance
.recordError(error, StackTrace.current, attributes: attributes);
}

Syntax

RecordException(System.Exception exception) : void;

Description

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

Parameters

Parameter

Type

Description

exception

Exception

Required. The exception to be recorded.

Return values

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

Example

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

Syntax

RecordException(System.Exception exception) : void;

Description

Manually record any handled exceptions.

Parameters

Parameter

Type

Description

exception

Exception

Required. The exception to be recorded.

Return values

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

Example

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

Syntax

RecordException(System.Exception exception) : void;

Description

Manually record any handled exceptions.

Parameters

Parameter

Type

Description

exception

Exception

Required. The exception to be recorded.

Return values

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

Example

try {
some_code_that_throws_error();
} catch (Exception ex) {
CrossNewRelicClient.Current.RecordException(ex);
}
Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.