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

try-catch-else #21130

Closed
christopher-dG opened this issue Mar 21, 2017 · 12 comments · Fixed by #42211
Closed

try-catch-else #21130

christopher-dG opened this issue Mar 21, 2017 · 12 comments · Fixed by #42211
Labels
feature Indicates new feature / enhancement requests help wanted Indicates that a maintainer wants help on an issue or pull request

Comments

@christopher-dG
Copy link
Member

christopher-dG commented Mar 21, 2017

Coming from Python, I was really surprised to find that Julia has no way to execute code after a try block only if no exception/error was caught. Right now it seems the only way to get around this is something like:

try
  maybe_raise_exception()
  unrelated_stuff()
  more_unrelated()
catch
  stuff()
end 

Whereas the Python code for this would read:

try:
  maybe_raise_exception()
except SomeException:
  stuff()
else:
  unrelated_stuff()
  more_unrelated() 

It seems like bad practice to put code that we aren't worried about causing exceptions/errors in a try block so I'm interested in hearing what others have to say about this.

e: Alternatively you could do:

try
  maybe_raise_exception()
  noex = true
catch
  stuff()
end
if noex
  unrelated_stuff()
  more_unrelated()
end

Although I do personally prefer the else syntax.

@JeffBezanson
Copy link
Member

Yes, this seems like a good feature.

@yuyichao
Copy link
Contributor

Ref #1289 which is somewhat more confusing than try-catch-else....

@ararslan ararslan added design Design of APIs or of the language itself speculative Whether the change will be implemented is speculative labels Mar 21, 2017
@stevengj
Copy link
Member

stevengj commented Mar 22, 2017

I don't quite see the problem with

try
  maybe_raise_exception()
  unrelated_stuff()
  more_unrelated()
catch
  stuff()
end 

There's no code duplication, and it seems like this is a normal way to use a try block: the whole block executes only if there is no exception.

Is the concern that you want to do something different if unrelated_stuff() throws? And is that situation common enough to be worth a language feature (instead of setting a flag)?

@yuyichao
Copy link
Contributor

I think the issue is that unrelated_stuff() and more_unrelated() can throw exceptions that you don't want to catch since they are "unrelated".

@christopher-dG
Copy link
Member Author

What @yuyichao said. Additionally, I don't think that code that has no risk of throwing an exception should be in a try block.

@StefanKarpinski
Copy link
Member

See also #7026. The whole reason for try-catch-else is so that you don't accidentally catch an unexpected error thrown by unrelated_stuff that just happens to look like the expected error thrown by maybe_raise_exception. In the chain of custody design, that's not possible since you'd need to annotate maybe_raise_exception with throws SomeExceptionType in order to be able to catch it the exception it throws. It seems to me that this is a feature that just sweeps the real problem of catching the wrong exception under the carpet.

@JeffBezanson
Copy link
Member

I think this is a reasonable feature idea. I can see how it's somewhat redundant with the chain-of-custody design, but handles a much simpler and more limited case. It's not really necessary to be able to catch exceptions with fine granularity when all you need is to pop the exception handler for an easily-identified block of code.

@christopher-dG
Copy link
Member Author

Sure, I was just closing due to lack of activity. Maybe we can see it in the future 🙂.

@JeffBezanson
Copy link
Member

Yes, there have definitely been cases where we implemented a feature literally years after it was requested. This one might not even take that long :-P

@heetbeet
Copy link

heetbeet commented May 6, 2021

I developed this for myself to get around some of the try/catch problems I encountered:
https://github.com/heetbeet/TryCatch.jl

@StefanKarpinski
Copy link
Member

There doesn't seem to be any real objection to this feature, someone just has to implement it. I've occasionally wanted this.

@StefanKarpinski StefanKarpinski added feature Indicates new feature / enhancement requests help wanted Indicates that a maintainer wants help on an issue or pull request and removed design Design of APIs or of the language itself speculative Whether the change will be implemented is speculative labels May 7, 2021
@StefanKarpinski
Copy link
Member

I've removed the design and speculative labels since there doesn't seem to be much to design: allow an else block on a try-catch that is evaluated if nothing is caught. Doesn't seem especially speculative either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Indicates new feature / enhancement requests help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants