# Trace\_on\_error

**URL:** <https://swi-prolog.discourse.group/t/trace-on-error/1333>\
**Category:** General\
**Created:** [September 30, 2019, 8:08pm UTC](https://swi-prolog.discourse.group/t/trace-on-error/1333 "2019-09-30T20:08:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [September 30, 2019, 8:08pm UTC](https://swi-prolog.discourse.group/t/trace-on-error/1333/1 "2019-09-30T20:08:51Z")

</div>

My program is generating this:

error(type\_error(evaluable,/0),context(system:(=\<)/2,\_846812))

I don’t know which spot is causing this, or the call tree. I would like to start the tracer when an error occurs so I can examine where it’s at. trace\_on\_error does not seem to be a predicate though. For what it’s worth, there are more than one catch calls in this stack, so some suggestions are not viable, and setting debug=true for the duration is also not always viable, though sometimes it is.

---

<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:** [October 1, 2019, 8:12am UTC](https://swi-prolog.discourse.group/t/trace-on-error/1333/2 "2019-10-01T08:12:09Z")

</div>

Use the [gui tracer](https://www.swi-prolog.org/pldoc/man?section=guitracer) and edit/exceptions or Prolog/Edit exceptions from the [PceEmacs](https://www.swi-prolog.org/pldoc/man?section=pceemacs) editor windows. That does run in debug mode. If you cannot afford that, it should produce a backtrace for uncaught exceptions. If the exception is caught you can use [catch\_with\_backtrace/3](https://www.swi-prolog.org/pldoc/doc_for?object=catch_with_backtrace/3) instead of [catch/3](https://www.swi-prolog.org/pldoc/doc_for?object=catch/3) or you can extend the guard definition for [library(prolog\_stack)](https://www.swi-prolog.org/pldoc/doc/_SWI_/library/prolog_stack.pl) ([GitHub](https://github.com/SWI-Prolog/swipl-devel/blob/master/library/prolog_stack.pl)). See the library.

---

<div class="post-metadata">

**Author:** ![abaljeu](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/abaljeu/32/136_2.png) [@abaljeu](https://swi-prolog.discourse.group/u/abaljeu)\
**Post date:** [October 1, 2019, 1:40pm UTC](https://swi-prolog.discourse.group/t/trace-on-error/1333/3 "2019-10-01T13:40:03Z")

</div>

Edit Exceptions in the prolog editor. Who knew that existed and had error defined to trace “if not caught”?

catch\_with\_backtrace is also handy.

---

<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:** [October 2, 2019, 8:51am UTC](https://swi-prolog.discourse.group/t/trace-on-error/1333/4 "2019-10-02T08:51:40Z")

</div>

> [@abaljeu](#):
>
> catch\_with\_backtrace is also handy

Note that you should only use this if you want to print the exception. If you catch it do do something else you should be aware that getting the backtrace into the exception is pretty expensive, in particular if source code locations are enabled (default).
