# Catch warnings

**URL:** <https://discourse.julialang.org/t/catch-warnings/29603>\
**Category:** General Usage\
**Created:** [October 7, 2019, 3:51pm UTC](https://discourse.julialang.org/t/catch-warnings/29603 "2019-10-07T15:51:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [October 7, 2019, 3:51pm UTC](https://discourse.julialang.org/t/catch-warnings/29603/1 "2019-10-07T15:51:57Z")

</div>

How could I catch if a block of code issues some warning? Something like `try` - `catch` for warnings.

Or alternatively, how make warnings throw an error?

---

<div class="post-metadata">

**Author:** ![pjgorski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjgorski/32/4802_2.png) [@pjgorski](https://discourse.julialang.org/u/pjgorski)\
**Post date:** [March 19, 2020, 11:17pm UTC](https://discourse.julialang.org/t/catch-warnings/29603/2 "2020-03-19T23:17:33Z")

</div>

It is not a general solution, but in my case a function generating warning did it after handling an error, so running `rethrow()` and checking the error details allowed me to properly handle it.

In my case the warning was related with the following error:  
`ERROR: UndefVarError: SparseArrays not defined`

So the code as follows handled it:

```julia
#first throw any error for the rethrow not to catch the previous considered error
try
        throw("")
    catch error
end
#run code that issues the warning
(...)
#check whether there was any error that issued the warning
try
	rethrow()
catch error
	if isa(error, UndefVarError)
		if error.var == Symbol(SparseArrays)
			throw(error)
		end
	end
end

```

---

<div class="post-metadata">

**Author:** ![tdb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tdb/32/44257_2.png) [@tdb](https://discourse.julialang.org/u/tdb)\
**Post date:** [November 9, 2022, 10:36pm UTC](https://discourse.julialang.org/t/catch-warnings/29603/3 "2022-11-09T22:36:46Z")

</div>

Does anyone have an idea of what to do if there is no accompanying error?
