Closed unary-code closed 1 year ago
That's not how Julia exceptions work. If quadgk
throws an exception, e.g. due to your integrand blowing up, then it does not have a return value.
You could do
integral, err = try
quadgk(....)
catch
Inf, Inf
end
if you want to use Inf
as the value when it throws an error, for example.
If you have further questions about exceptions and try
/catch
syntax, I would suggest https://discourse.julialang.org/
JuliaError: Exception 'UndefVarError:
integral
not defined' occurred while calling julia code:When I ran this code: " using QuadGK
try integral, err = quadgk(x -> exp(1/(x-0.5)), 0, 1, rtol=1e-8) catch print("Error") end print(integral) print(err) "
I am generating a bunch of expression trees somewhat randomly, where each expression tree represents a particular function of x. For example, one expression tree could be: / \ cos 5 | x0 which represents the function "cos(x0)5". I can call this function tree_as_func(x).
I need to run "quadgk(x -> tree_as_func(x), 0, 1, rtol=1e-8)" where "tree_as_func" is the function of x that the current expression tree represents. I do not know if "tree_as_func(x)" blows up to infinity on the domain x=[0, 1) or if its integral over x=[0, 1) is infinity.
As a result, I need to be able to catch errors. But this try-catch block fails to catch the error. How can I do this?