---
schema_version: "1"
id: ab20fefa1eb1d01a1dd474857be0eb5a6f3f3c18296fb8ea8a1b7332885e724a
title: Work With Throwables
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/java-module/latest/java-throwable.md
canonical_url: https://docs.mulesoft.com/java-module/latest/java-throwable
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2aafe3990aa0cc928ddffdf83549b89dc685698a9b8378c241162793a68e2b5e
description: MuleSoft Documentation Site
release: "2.0"
latest: true
---
---
product: Java Module
version: 2.0
is-latest-version: true
---

# Work With Throwables

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The Java module exposes useful DataWeave functions for throwable instances. (In Java, a throwable is
a `throw` statement.)

These functions have been available since DataWeave version 1.2.0.

## Check for a Cause

You can check if a throwable was caused by a certain type of exception using the `isCausedBy`
function. This is especially useful when adding logic to
[error handlers](https://docs.mulesoft.com/mule-runtime/latest/mule-error-concept).

```java
package com.me;

public class TaxCalculator {

	public Double calculateTax(Double price) {
		if (price == null) {
			throw new NullPointerException("Must provide a value for price");
		}
		if (price < 0) {
			throw new IllegalArgumentException("Price must be a non-negative value, but was: " + price);
		}
		return price * 0.21;
	}
}
```

The `calculateTax(Double price)` method declares the `IllegalArgumentException` and
`NullPointerException` exceptions.

The next code example lists part of a flow that throws an error during the execution of the
[`invoke` operation](https://docs.mulesoft.com/java-module/latest/java-invoke-method).

To check which of the Java exceptions occurred during the failed invocation of the
`calculateTax(Double price)` method, use the `isCausedBy` function in the error handler to add
different logic for each of the cases.

```xml
<java:new class="com.me.TaxCalculator"
	constructor="TaxCalculator()"
	target="taxCalculator"/>
<java:invoke instance="#[vars.taxCalculator]"
	class="com.me.TaxCalculator"
	method="calculateTax(Double)">
    <java:args>#[{price: payload.price}]</java:args>
</java:invoke>
<error-handler>
    <on-error-continue
    	when="#[Java::isCausedBy(error.cause, 'java.lang.IllegalArgumentException')]">
        <set-payload value="Negative prices are not allowed."/>
    </on-error-continue>
    <on-error-continue
    	type="#[Java::isCausedBy(error.cause, 'java.lang.NullPointerException')]">
        <set-payload value="There is no price."/>
    </on-error-continue>
</error-handler>
```

## Obtain a Root Cause

The `getRootCause` function obtains the root cause of a throwable, disregarding any exception
wrapping.

In the next example, all tax calculation errors are handled by the `FailureHandler` class, which has
the `handle(Throwable)` method that takes the root cause of the throwable. To make that possible,
the `getRootCause` function can be used to pass the root cause as an argument to the
`handle(Throwable)` method.

```xml
<java:new class="com.me.TaxCalculator"
  constructor="TaxCalculator()"
  target="taxCalculator"/>
<java:invoke instance="#[vars.taxCalculator]"
  class="com.me.TaxCalculator"
  method="calculateTax(Double)">
    <java:args>#[{price: payload.price}]</java:args>
</java:invoke>
<error-handler>
    <on-error-continue>
      <java:invoke-static
      	class="com.me.FailureHandler"
	method="handle(Throwable)">
          <java:args>#[{cause: Java::getRootCause(error.cause)}]</java:args>
      </java:invoke-static>
    </on-error-continue>
</error-handler>
```

## See Also

- [About the Mule Error](https://docs.mulesoft.com/mule-runtime/latest/mule-error-concept)
