# Throwing Exceptions in RPCHandler

**URL:** <https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496>\
**Category:** 3rd Party Modules\
**Created:** [December 30, 2014, 4:55pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496 "2014-12-30T16:55:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssmith](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@ssmith](https://forum.inductiveautomation.com/u/ssmith)\
**Post date:** [December 30, 2014, 4:55pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/1 "2014-12-30T16:55:21Z")

</div>

I’ve set up an RPCHandler like in the Ignition Programmer’s Guide. If I try to throw an exception to be caught on a component from the RPCHandler then things go wrong. Both on the RPCHandler and on the interface it implements I’ve specified “throws MyException”. I think at some point my thrown exception gets wrapped as a GatewayException. I’m guessing because my RPCHandler doesn’t specify that it throws GatewayExceptions (which I don’t think it’s even possible for it throw a gateway exception because GatewayException is defined in client scope), the GatewayException is then wrapped in an UndeclaredThrowableException. How can I get my original exception back to the function caller without unwrapping all the layers?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [December 30, 2014, 7:16pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/2 "2014-12-30T19:16:26Z")

</div>

I don’t think custom exception types can be propagated from gateway to client/designer through the module RPC system. Any exceptions caught have their cause, message, stack trace… extracted and sent to the client, where it re-assembles it as a new Throwable that I think you can find embedded in the GatewayException.

I think this all happens mostly due to some complex serialization issues.

---

<div class="post-metadata">

**Author:** ![ssmith](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@ssmith](https://forum.inductiveautomation.com/u/ssmith)\
**Post date:** [December 30, 2014, 9:23pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/3 "2014-12-30T21:23:13Z")

</div>

I see. Is it possible to specify all my functions in the RPCHandler throw GatewayExceptions so I can at least avoid the UndeclaredThrowableException? I tried to import com.inductiveautomation.ignition.client.gateway\_interface.GatewayException but the gateway complains it can’t be found. Is it a good idea to try to import GatewayException into my RPCHandler and see if I can adjust the build to make it work? Or is there some other way?

Assuming I can get the functions in my RPCHandler to throw GatewayExceptions, would you catch gatewayexceptions with something like this:

`try{
	rpc.doStuff()
} catch(GatewayException e){
	if(e.getCause()!=null){
		if(e.getCause() is MyException){
			//handle MyException
		}
		else if (e.getCause() is MyOtherException){
			//handle MyOtherException
		}
	}
}`

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 5, 2015, 6:31pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/4 "2015-01-05T18:31:01Z")

</div>

I’m not quite sure where the UndeclaredThrowableException is coming from - I don’t think it’s us.

If you change your handler so it just throws a vanilla Exception what happens?

---

<div class="post-metadata">

**Author:** ![ssmith](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@ssmith](https://forum.inductiveautomation.com/u/ssmith)\
**Post date:** [January 5, 2015, 8:26pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/5 "2015-01-05T20:26:55Z")

</div>

Kevin, you are correct. The UndeclaredThrowable isn’t from you guys (edit: not directly at least).

My function signature is:

```auto
void doStuff() throws MyException;
```

When calling rpc.doStuff(), java expects that it won’t throw a GatewayException so when it does throw a GatewayException, it wraps it in UndeclaredThrowableException.

I want my function’s signature to be:

```auto
void doStuff() throws GatewayException;
```

But I believe I cannot do that because GatewayException is a client-side thing.

And throwing Exception I think is considered bad practice because then anywhere that I call rpc.doStuff(), I either have to rethrow Exception everywhere or catch the original and throw a runtime Exception instead.

EDIT: I should add that throwing Exception does indeed stop the UndeclaredThrowableException, but it’s not ideal.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 5, 2015, 10:38pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/6 "2015-01-05T22:38:08Z")

</div>

It’s not ideal, but I think it’s all get you get with the RPC system - either your functions don’t throw at all, or they throw a generic Exception.

---

<div class="post-metadata">

**Author:** ![ssmith](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@ssmith](https://forum.inductiveautomation.com/u/ssmith)\
**Post date:** [January 6, 2015, 2:37pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/7 "2015-01-06T14:37:06Z")

</div>

I figured that might be the case ☹. Thanks for your help though 😃

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [January 6, 2015, 3:13pm UTC](https://forum.inductiveautomation.com/t/throwing-exceptions-in-rpchandler/9496/8 "2015-01-06T15:13:36Z")

</div>

One other workaround, kind of a hack, but still: Never throw from the RPC calls, and instead return a “result” object of your own creation, that can hold both a good/success result and a throwable/exception in the case of failure. The throwable inside your result object will not be touched by the RPC and serialization logic.
