# Is it possible to suppress the chain of 'caused by' output when handling an exception?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-suppress-the-chain-of-caused-by-output-when-handling-an-exception/107823
**Category:** General Usage
**Tags:** exception
**Created:** [December 19, 2023, 4:35pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-the-chain-of-caused-by-output-when-handling-an-exception/107823 "2023-12-19T16:35:57Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)
#### Post date: [December 19, 2023, 4:35pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-the-chain-of-caused-by-output-when-handling-an-exception/107823/1 "2023-12-19T16:35:57Z")

</div>

When handling an exception that was ‘caused by’ another exception, Julia prints out a stack of exceptions. Normally this is helpful, but in this case I want to handle the exception, and give the user a helpful message. I can do this, but after my error message is printed, there is the original exception which (in this case) makes it harder for the user to find the message I want them to see.

For example

```julia
julia> function foo()
       try
       Base.require(Main, :NCDatasets)
       catch
       error("To handle this error, do X")
       end
       end
foo (generic function with 1 method)

julia> foo()
ERROR: To handle this error, do X
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] foo()
   @ Main ./REPL[11]:5
 [3] top-level scope
   @ REPL[12]:1

caused by: ArgumentError: Package NCDatasets not found in current path, maybe you meant `import/using .NCDatasets`.
- Otherwise, run `import Pkg; Pkg.add("NCDatasets")` to install the NCDatasets package.
Stacktrace:
 [1] macro expansion
   @ ./loading.jl:1630 [inlined]
 [2] macro expansion
   @ ./lock.jl:267 [inlined]
 [3] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:1611
 [4] foo()
   @ Main ./REPL[11]:3
 [5] top-level scope
   @ REPL[12]:1

```

How can I get rid of the `caused by: ArgumentError` block (which in my actual use case has a long stack trace which pushes my error message off the top of the screen)?

PS I do want to raise an error that stops execution (but does not exit Julia) from far down a call stack, so I do not want (for example) to just print a message and return from the function.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [December 19, 2023, 5:01pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-the-chain-of-caused-by-output-when-handling-an-exception/107823/2 "2023-12-19T17:01:05Z")

</div>

> **[GitHub - BioTurboNick/AbbreviatedStackTraces.jl](https://github.com/BioTurboNick/AbbreviatedStackTraces.jl)**
>
> Contribute to BioTurboNick/AbbreviatedStackTraces.jl development by creating an account on GitHub.

Edit: I thought you were asking a different question. One second …

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [December 19, 2023, 5:16pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-the-chain-of-caused-by-output-when-handling-an-exception/107823/3 "2023-12-19T17:16:40Z")

</div>

Is this related to the difference between `throw` and `rethrow`? I’m a little bit unclear on that myself

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [December 19, 2023, 5:20pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-the-chain-of-caused-by-output-when-handling-an-exception/107823/4 "2023-12-19T17:20:27Z")

</div>

Yes, `rethrow` is one option here. But: the docs admonish you about _not_ using it because it “lies” about what caused the problem 🙂

> The alternative form `rethrow(e)` allows you to associate an alternative exception object `e` with the current backtrace. However this misrepresents the program state at the time of the error so you’re encouraged to instead throw a new exception using `throw(e)`.

```julia-repl
julia> function foo()
       try
       Base.require(Main, :NCDatasets)
       catch ex
       rethrow(ArgumentError("To handle this error, do X"))
       end
       end
foo (generic function with 1 method)

julia> foo()
ERROR: ArgumentError: To handle this error, do X
Stacktrace:
 [1] macro expansion
   @ ./loading.jl:1630 [inlined]
 [2] macro expansion
   @ ./lock.jl:267 [inlined]
 [3] require(into::Module, mod::Symbol)
   @ Base ./loading.jl:1611
 [4] foo()
   @ Main ./REPL[17]:3
 [5] top-level scope
   @ REPL[18]:1

```

^^ This stack trace points at `require`, not the catch block of `foo()`!
