# Attach additional information to exception

**URL:** <https://discourse.julialang.org/t/attach-additional-information-to-exception/105337>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 24, 2023, 7:05am UTC](https://discourse.julialang.org/t/attach-additional-information-to-exception/105337 "2023-10-24T07:05:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [October 24, 2023, 7:05am UTC](https://discourse.julialang.org/t/attach-additional-information-to-exception/105337/1 "2023-10-24T07:05:19Z")

</div>

If I want to attach additional information to an exception, e.g. I have a vector of some structs and do something with each. For one, there is an exception, and it would be helpful if I know from which.

Maybe like this:

```julia
julia> try
           try
             Int64(NaN)
         catch e
             error("Error from struct 1")
         end
       catch e2
           error("Error 2")
       end
ERROR: "Error 2"
Stacktrace:
 [1] top-level scope
   @ REPL[9]:8

caused by: "Error from struct 1"
Stacktrace:
 [1] top-level scope
   @ REPL[9]:5

caused by: InexactError: Int64(NaN)
Stacktrace:
 [1] Int64(x::Float64)
   @ Base .\float.jl:900
 [2] top-level scope
   @ REPL[9]:3

```

Is there an idiomatic way for this?

Mixing logging and exceptions is an option too:

```julia
try
    Int64(NaN)
catch e
    @debug("Error from struct 1")
    throw(e)
end

```

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [October 24, 2023, 7:20am UTC](https://discourse.julialang.org/t/attach-additional-information-to-exception/105337/2 "2023-10-24T07:20:29Z")

</div>

Your best bet is to define your own exception types.

```julia
julia> struct MyException <: Exception
           arg
       end

julia> throw(MyException(1))
ERROR: MyException(1)

```

This is just a plain struct, so it can have arbitrary number of fields, and of couse the fields can have type specifiers as well.

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [October 24, 2023, 3:00pm UTC](https://discourse.julialang.org/t/attach-additional-information-to-exception/105337/3 "2023-10-24T15:00:44Z")

</div>

How would I then know the root cause?

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [October 24, 2023, 4:58pm UTC](https://discourse.julialang.org/t/attach-additional-information-to-exception/105337/4 "2023-10-24T16:58:18Z")

</div>

I guess this is the correct way: [GitHub - JuliaServices/ExceptionUnwrapping.jl: "I started by producing, and the rapping came second to that, because I wanted to fill out the beat." - Awkwafina](https://github.com/JuliaServices/ExceptionUnwrapping.jl)

```julia
struct FooException{T} <: Base.Exception
    captured_exception::T
    msg::String
end

```

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [October 25, 2023, 6:34am UTC](https://discourse.julialang.org/t/attach-additional-information-to-exception/105337/5 "2023-10-25T06:34:39Z")

</div>

You don’t need to explicitly capture the root cause, Julia takes care of that for you when you throw an exception from a catch block. You can fetch the stack of exceptions with `Base.current_exceptions()`, where you will find not only your custom exception, but the root cause(s) as well.
