Catch warnings

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?

1 Like

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:

#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

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