# Catch / throw example with custom error

**URL:** <https://swi-prolog.discourse.group/t/catch-throw-example-with-custom-error/290>\
**Category:** Help!\
**Created:** [February 17, 2019, 8:41am UTC](https://swi-prolog.discourse.group/t/catch-throw-example-with-custom-error/290 "2019-02-17T08:41:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 17, 2019, 8:41am UTC](https://swi-prolog.discourse.group/t/catch-throw-example-with-custom-error/290/1 "2019-02-17T08:41:55Z")

</div>

Hi,

I am reading the throw/1 documentation and expected error term and can’t figure it out / get it to work. Could someone post a simple example how to handle a custom error via a corresponding catch and throw

… no idea how iso errors fit into this as well …

scanning the list of iso error types, none really fit to what the error means – its a semantic kind of error, specific to the algorithm i am working on.

any help would be much appreciate,d

Dan

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 17, 2019, 9:00am UTC](https://swi-prolog.discourse.group/t/catch-throw-example-with-custom-error/290/2 "2019-02-17T09:00:53Z")

</div>

btw, here is what i do and that doesnt work

```prolog
p0(X) :-
 catch(p1(X), myError(Data), recover(Data)).

```

then somehwere later …

```prolog
q(Y) :- 
  condition(Y),
  throw(myError(Y)).

```

when run, the throw gets me an "Unknown message myError(abc)

with ‘abc’ being the bound value to Y.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [February 17, 2019, 9:22am UTC](https://swi-prolog.discourse.group/t/catch-throw-example-with-custom-error/290/3 "2019-02-17T09:22:50Z")

</div>

oh,

it looks like it actually worked … i misinterpreted the debug message

Dan

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 17, 2019, 1:50pm UTC](https://swi-prolog.discourse.group/t/catch-throw-example-with-custom-error/290/4 "2019-02-17T13:50:08Z")

</div>

There are two related things.

- Exceptions of the from `error(Formal, Context)` are special. If Context is left unbound the system will fill it with a stack trace if the exception is not caught and the system will try to start the debugger as ealy as possible in an interactive session.
- Define rules for prolog:error\_message//1 to print error/2 messages based on  
the first argument or prolog:message//1 to handle any exception term.
