# CreateFolder Try with no Catch

**URL:** <https://forum.xojo.com/t/createfolder-try-with-no-catch/79825>\
**Category:** General\
**Created:** [April 2, 2024, 10:43am UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825 "2024-04-02T10:43:53Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 2, 2024, 10:43am UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/1 "2024-04-02T10:43:53Z")

</div>

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

```auto
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

```auto
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](https://toroco.fr/DocsTempPub/CreateFolder_NotHandleError.zip) if you want to try.  
I’m under macOS BigSur 11.7.10 with Xojo 2024r1.

---

<div class="post-metadata">

**Author:** ![Jean-Yves\_Pochez](https://forum.xojo.com/user_avatar/forum.xojo.com/jean-yves_pochez/32/21769_2.png) [@Jean-Yves\_Pochez](https://forum.xojo.com/u/Jean-Yves_Pochez)\
**Post date:** [April 2, 2024, 11:45am UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/2 "2024-04-02T11:45:36Z")

</div>

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…

 ![Capture d’écran 2024-04-02 à 13.43.36](https://forum.xojo.com/uploads/default/original/3X/6/b/6bb788ae63d20fa42a8a8b5b2e6884c8b5cebe7b.png)

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`

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 2, 2024, 12:19pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/3 "2024-04-02T12:19:17Z")

</div>

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](https://documentation.xojo.com/api/language/try.html)

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

```auto
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](https://documentation.xojo.com/api/language/catch.html) Optional. This code executes should an exception occur. The optional _errorParameter_ allows you to be specific about the type of exception ([IOException](https://documentation.xojo.com/api/exceptions/ioexception.html), [RuntimeException](https://documentation.xojo.com/api/exceptions/runtimeexception.html), 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

```auto
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](https://documentation.xojo.com/api/language/catch.html) This code executes should an exception occur. The optional _errorParameter_ allows you to be specific about the type of exception ([IOException](https://documentation.xojo.com/api/exceptions/ioexception.html), [RuntimeException](https://documentation.xojo.com/api/exceptions/runtimeexception.html), 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.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 2, 2024, 12:33pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/4 "2024-04-02T12:33:46Z")

</div>

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**.

```auto
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](https://tracker.xojo.com/xojoinc/xojo/-/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).

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [April 2, 2024, 4:32pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/5 "2024-04-02T16:32:00Z")

</div>

> [@AlbertoD](#):
>
> Reading the docs it says Catch is optional.

That’s clearly incorrect, as Thomas proved.

> [@AlbertoD](#):
>
> Having the IDE warn that we have a missing Catch will be a plus.

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”).

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 2, 2024, 4:45pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/6 "2024-04-02T16:45:22Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> I never understood what’s the difference between a code just after the **Finally** and a code after the **End Try**

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:

```auto
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")

```

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 2, 2024, 7:34pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/7 "2024-04-02T19:34:25Z")

</div>

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

```auto
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")

```

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 2, 2024, 7:42pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/8 "2024-04-02T19:42:58Z")

</div>

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):

```auto
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:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Christian\_Wheel](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_wheel/32/20536_2.png) [@Christian\_Wheel](https://forum.xojo.com/u/Christian_Wheel)\
**Post date:** [April 2, 2024, 9:08pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/9 "2024-04-02T21:08:08Z")

</div>

_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.

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 2, 2024, 9:27pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/10 "2024-04-02T21:27:30Z")

</div>

> [@Christian\_Wheel](#):
>
> _Catch_ is indeed optional

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.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [April 2, 2024, 9:39pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/11 "2024-04-02T21:39:06Z")

</div>

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

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Thomas\_ROBISSON](https://forum.xojo.com/user_avatar/forum.xojo.com/thomas_robisson/32/15868_2.png) [@Thomas\_ROBISSON](https://forum.xojo.com/u/Thomas_ROBISSON)\
**Post date:** [April 3, 2024, 11:09am UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/12 "2024-04-03T11:09:07Z")

</div>

Thank you Alberto and other, I understand a little better.  
I updated my sample project [CreateFolder\_NotHandleError](https://toroco.fr/DocsTempPub/CreateFolder_NotHandleError.zip)

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:

```auto
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:

```auto
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:

```auto
Dim TpNbre as Int16

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

```

---

<div class="post-metadata">

**Author:** ![Arnaud\_N](https://forum.xojo.com/user_avatar/forum.xojo.com/arnaud_n/32/21824_2.png) [@Arnaud\_N](https://forum.xojo.com/u/Arnaud_N)\
**Post date:** [April 3, 2024, 12:03pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/13 "2024-04-03T12:03:22Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> I moved the divide / 0 outside the Try and there isn’t any error!!!??? I don’t understand.

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

---

<div class="post-metadata">

**Author:** ![AlbertoD](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@AlbertoD](https://forum.xojo.com/u/AlbertoD)\
**Post date:** [April 3, 2024, 12:04pm UTC](https://forum.xojo.com/t/createfolder-try-with-no-catch/79825/14 "2024-04-03T12:04:50Z")

</div>

> [@Thomas\_ROBISSON](#):
>
> I divide by 0 and there was no error

This may help:

> [@Xojo divides by ZERO?](https://forum.xojo.com/t/xojo-divides-by-zero/69235/5):
>
> Xojo is not a language that has ever raised an exception for divide by 0. Docs say: If you divide by zero, the result is positive or negative infinity, except if the numerator is also zero. In that case, the result is NaN. For that reason, you should always check to see whether the denominator is zero before using / .
