Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not catch a JuliaError caused by function blowing up to infinity #81

Closed
unary-code opened this issue Jul 4, 2023 · 2 comments
Closed

Comments

@unary-code
Copy link

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?

@stevengj
Copy link
Member

stevengj commented Jul 4, 2023

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.

@stevengj stevengj closed this as completed Jul 4, 2023
@stevengj
Copy link
Member

stevengj commented Jul 4, 2023

If you have further questions about exceptions and try/catch syntax, I would suggest https://discourse.julialang.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants