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 without catch should not catch #27554

Closed
oxinabox opened this issue Jun 13, 2018 · 5 comments · Fixed by #27559
Closed

try without catch should not catch #27554

oxinabox opened this issue Jun 13, 2018 · 5 comments · Fixed by #27559
Assignees
Labels
kind:breaking This change will break code parser Language parsing and surface syntax

Comments

@oxinabox
Copy link
Contributor

oxinabox commented Jun 13, 2018

This is a breaking change,
but I feel like I should point it out now while 0.7 is still alpha,
as there is a chance it can be fixed before 1.0

Right now: if you have a try block that has neither a catch nor a finally,
it is the same as a try block with a catch.
But if you have a try block without a catch and with a finally it does not catch anything.

My gut tells me that without a catch nothing should be caught.
and also that adding an empty finally block should never change behaviour.

Right now the tryblock comes in 4 forms:

try-catch-finally:

Catches (what I expected)

julia> try
               error("NOOO")
       catch

       finally

       end

julia> 

try-catch

Catches (what I expected)

julia> try
               error("NOOO")
       catch

       end

julia>

try-finally

Does Not Catch (what I expected)

julia> try
               error("NOOO")
       finally

       end
ERROR: NOOO
Stacktrace:
 [1] anonymous at ./<missing>:?

julia> 

try

Catches (Not what I expected)

julia> try
               error("NOOO")
       end

julia>

This caught me off-guard today,
because I had some code that I had added a catch block to, for debugging purposes.
I was using it as a big of a hack dump out some extra state to the screen so I could find what was wrong with my code.

When I was done debugging, I commented out the catch block,
because I didn't want to catch things anymore.

Many weeks later (today) I come back to the code, and I do something kinda silly for other debugging reasons (printing an undefined variable)
and I am sitting here wondering first "Why am i not getting the expected output", and then once I found my mistake "Why is my program not erroring out?"

@martinholters
Copy link
Member

IIUC correctly, wrapping a block in try ... end without catch or finally should be equivalent to just the wrapped block? Is there then any need for it? Should we just disallow it?

@oxinabox
Copy link
Contributor Author

oxinabox commented Jun 13, 2018

IIUC correctly, wrapping a block in try ... end without catch or finally should be equivalent to just the wrapped block?

Yes, that is what I am saying.

Should we just disallow it?

Could do:

  • Java disallows it
  • C# disallows it as a specific named error
  • Python can't even parse it and treats it as incomplete statement (which I guess it is)

So just plain making it a syntax error would be reasonable

@JeffBezanson
Copy link
Sponsor Member

Yes, I think we can disallow it.

@JeffBezanson JeffBezanson added kind:breaking This change will break code parser Language parsing and surface syntax labels Jun 13, 2018
@JeffBezanson JeffBezanson self-assigned this Jun 13, 2018
@Ken-B
Copy link
Contributor

Ken-B commented Jun 13, 2018

FWIW, I like the try ... end syntax. It allows me to try some code over many datasets without having to worry about data quality up front and get a feel for the results. In production code I would use catch (at least for logging) but for exploration the try ... end is convenient.
(It's also easier for a one-liner).
In @oxinabox 's debugging story, I would rather comment out the try than just the catch, but it seems we have different expectations from a try keyword.

@StefanKarpinski
Copy link
Sponsor Member

julia> @eval macro $(:try)(ex)
           quote
               try $(esc(ex))
               catch
               end
           end
       end
@try (macro with 1 method)

julia> @try error("BOO")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:breaking This change will break code parser Language parsing and surface syntax
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants