CreateFolder Try with no Catch

Hi, I consider this a bug but I prefer to ask before fill a bug report.

Try
  f_Elt.CreateFolder
Catch
  MessageBox "Error while creating folder" ' This is not displayed even if error occurs
End Try

If f_Elt.parent is a lock folder, I have an error and then the MessageBox. It’s ok for me.
But if I do

Try
  f_Elt.CreateFolder
' Catch
  ' MessageBox "Error while creating folder" ' This is not displayed even if error occurs
End Try

The application crash if built or in Debugmode. I think that if with put a Try - End Try, the error should be handled even if we don’t need the Catch.
If the Catch is necessary, then the IDE should alert “Catch statement is missing.”
Download the sample project CreateFolder_NotHandleError if you want to try.
I’m under macOS BigSur 11.7.10 with Xojo 2024r1.

Thomas,
you must uncheck “break on exception” in the xojo project menu
or in debug mode you will always stop on exception, with catch or without…

you can also use #pragma breakonexceptions false (and then true) to disactivate temporarely this break.

and for the catch you should use a variable to catch the error
catch e as ioerrorexception

I always assumed the Try needs a Catch. Before reading the docs all the samples and posts in the forum show Try…Catch.

Reading the docs it says Catch is optional:
https://documentation.xojo.com/api/language/try.html

Maybe this is a documentation bug and they should change it from

Try
  ' Your code
Catch [ errorParameter [ As ErrorType ] ]
  ' Your code to handle the exception
Finally
  ' Your code that will execute even if an exception has occurred
End Try

Catch Optional. This code executes should an exception occur. The optional errorParameter allows you to be specific about the type of exception (IOException, RuntimeException, etc.) you wish to catch. The errorType is also optional but can only be used in conjunction with an ErrorParameter. A Try Catch statement can continue multiple Catch statements that handle different types of exceptions.

to

Try
  ' Your code
Catch [ errorParameter [ As ErrorType ] ]
  ' Your code to handle the exception
[ Finally ]
  ' Your code that will execute even if an exception has occurred
End [ Try ]

Catch This code executes should an exception occur. The optional errorParameter allows you to be specific about the type of exception (IOException, RuntimeException, etc.) you wish to catch. The errorType is also optional but can only be used in conjunction with an ErrorParameter. A Try Catch statement can continue multiple Catch statements that handle different types of exceptions.

To make it clear that Catch needs to be used with Try.

Having the IDE warn that we have a missing Catch will be a plus.

Edit: Jean-Yves, that is not the problem, the problem is that a Try without a Catch will fail because the error was not handled.

By the way, I never used because I never understood what’s the difference between a code just after the Finally and a code after the End Try .

Try
  ' Your code
Catch [ errorParameter [ As ErrorType ] ]
  ' Your code to handle the exception
[ Finally ]
  ' Your code that will execute even if an exception has occurred
End [ Try ]
' The code here will execute too even if an exception has occurred

Then I will fill a bug report issues #76011, thank you Alberto and Jean-Yves (but effectively my problem was not that the code stop, the problem is the code does not continue even if in a Try - End Try).

That’s clearly incorrect, as Thomas proved.

Given the fact that a block of “try”/”End try” without “Catch” behaves like if there was no “try” block at all, I’d agree that a compiler error could be thrown (like when an “if” line doesn’t end with “then”).

Code after Finally will execute even if the exception was not handled. For example, we have code with a catch for NilObjectException but the exception is RuntimeException:

Try
  Raise New RuntimeException
Catch e As NilObjectException
Finally
  ' Executes even though the exception is not handled
  MessageBox("Finally")
End Try
' this code will not execute
MessageBox("After End Try")

Thank you Alberto, I write it differently just to be sure I understood correctly.

Try
  MyLineCode
Catch e As NilObjectException
  MessageBox("This code executes if the error is a NilObjection and the code will continue after the End Try")
Finally
  MessageBox("This code executes if there is an error other than a NilObjection and the code stop here as if it executes a Return")
End Try
MessageBox("Code after End Try")
1 Like

I think that Finally will always execute.
No error, catched error or uncatched error.

If error is catched or no error, Finally will execute and then your code will continue (not “the code stop here as if it executes a Return”)

This code will show 3 message boxes (one after the other):

Try
  Raise New NilObjectException
Catch e As NilObjectException
  MessageBox("This code executes if the error is a NilObjection and the code will continue after the End Try")
Finally
  MessageBox("This code executes if there is an error other than a NilObjection and the code stop here as if it executes a Return")
End Try
MessageBox("Code after End Try")

this code will only show one of our MessageBox (Finally one) before showing another that we didn’t handle an error and it needs to shut down:

Try
  Raise New RuntimeException
Catch e As NilObjectException
  MessageBox("This code executes if the error is a NilObjection and the code will continue after the End Try")
Finally
  MessageBox("This code executes if there is an error other than a NilObjection and the code stop here as if it executes a Return")
End Try
MessageBox("Code after End Try")

This code will execute Finally MessageBox and MessageBox after End Try:

Try
  Var test As String
Catch e As NilObjectException
  MessageBox("This code executes if the error is a NilObjection and the code will continue after the End Try")
Finally
  MessageBox("This code executes if there is an error other than a NilObjection and the code stop here as if it executes a Return")
End Try
MessageBox("Code after End Try")

I hope is clear now.

Catch is indeed optional, in that excluding it will not yield a syntaxical error (ie not like excluding the then from an if statement). But you can’t expect an error to be “caught” without a catch. The try block simply denotes the start and end of a section of code to be handled by a particular catch and is not a catch in-and-of-itself.

Correct. A try block without a catch is something that is not practical. Similar to a Select case without case statements.

I guess OP’s request is that catch is needed to be practical (for a try block) then a little help from the IDE at compile time will be helpful. Maybe is too hard to implement.

It appears that try/end try without a catch functions similarly to if true/end if, in that it creates a new scope:

dim x as integer = 1
try
  dim y as integer = 2
end try
MessageBox str(y)  // Compile error : y does not exist

is the same as

dim x as integer = 1
if true then
  dim y as integer = 2
end if
MessageBox str(y)  // Compile error : y does not exist

Sometimes I use if true for that purpose, but I’ve never considered using try - I think that might be confusing.

2 Likes

Thank you Alberto and other, I understand a little better.
I updated my sample project CreateFolder_NotHandleError

For the other like me who didn’t correctly understand, you can select PopupMenu2 click the Button2 “Error specific and other” ans see what happens.

The Button3 “Error Specific and Finally” show me the Finally difference (thanks again Alberto).

When I was trying to make another error than Nil, I divide by 0 and there was no error. I first searched what I did wrong in my Catch but I didn’t find. I moved the divide / 0 outside the Try and there isn’t any error!!!??? I don’t understand.

Here is the code in my Button2:

Dim f_elt as FolderItem
Dim TpNbre, TabNbre(2) as Int16

Try
  Select Case PopupMenu2.SelectedRowIndex
  Case 0 ' No Error
    tPNbre = 6
  Case 1 ' Error Catch e (specifically)
    f_elt = f_elt.Child("rrtyr") ' Nil objection error
  Case 2 ' Error Catch (other)
    TabNbre(3) = 6
  Case 3 ' Raise New RuntimeException
    Raise New RuntimeException
  Case 4 ' No Divide by 0 error
    TpNbre = 6 / 0 ' Divide by 0 error
    MessageBox "I just divide a number by 0 and I have no error !"
  End Select
Catch e As NilObjectException
  MessageBox("Catch e")
Catch
  MessageBox("Catch other")
Finally
  MessageBox("Finally")
End Try
MessageBox("Code after End Try")

.

Here is the code in my Button3:

Dim TabNbre(2) as Int16

Try
  TabNbre(3) = 6
  ' Raise New RuntimeException
Catch e As NilObjectException
  MessageBox("Catch e")
  ' Catch
  ' MessageBox("Catch other")
Finally
  MessageBox("Finally")
End Try
MessageBox("We don't see this MessageBox as the error was not handled")

.

Here is the code in my Button4:

Dim TpNbre as Int16

TpNbre = 6 / 0
MessageBox str(TpNbre) ' Result is 0 !!!???

Arithmetics operations don’t trigger exceptions in Xojo. That’s always been so (even discussed in this forum).

This may help: