Confused - Do I need just "Exception" or Try Catch also

While I was trying to understand how to write code for the “deprecation” of “WriteError”, I noticed there wasn’t a Try Catch in the code.
I went looking for an explanation of when I need a Try Catch set of statements versus just Exception.
When I looked at Try Catch, it said for RuntimeException errors.

IOException is on that page, so I followed its link and got back to the beginning of the circle.

So. What’s the difference?

Here’s the code:

[code]Var f As FolderItem = FolderItem.ShowSaveFileDialog(FileTypes1.Text, “Create Example.txt”)
If f <> Nil Then
Var t As TextOutputStream = TextOutputStream.Create(f) // this line could raise an IOException.
t.WriteLine(TextField1.Value)
End If

Exception err As IOException
MsgBox(“an IO exception occurred”)[/code]

When you use try catch its often in the middle of code and there may be more code following the catch you want to continue executing
So you might see

   ... some bunch of code ...
   try
        <code that might raise an exception like an io error etc>
   catch exc as <some type of exception>
   end try
   ... some bunch of code ...

but then other times you have a tiny bit of code that doesnt have other code to follow so you just want to catch ALL exceptions and deal with them

You COULD write

try
  Var f As FolderItem = FolderItem.ShowSaveFileDialog(FileTypes1.Text, "Create Example.txt")
  If f <> Nil Then
    Var t As TextOutputStream = TextOutputStream.Create(f)  // this line could raise an IOException.
    t.WriteLine(TextField1.Value)
  End If

catch exc err As IOException
  MsgBox("an IO exception occurred")

end try

Or, since the entire method is inside the TRY CATCH you can use the syntax you see where there is no TRY and ANY exception that is thrown by any line of code will jump to that single exception handler at the end as it the semantically the same as putting the entire body of the method in a try catch block

Var f As FolderItem = FolderItem.ShowSaveFileDialog(FileTypes1.Text, "Create Example.txt")
If f <> Nil Then
  Var t As TextOutputStream = TextOutputStream.Create(f)  // this line could raise an IOException.
  t.WriteLine(TextField1.Value)
End If

Exception err As IOException
  MsgBox("an IO exception occurred")

PERSONALLY I prefer to explcitily write the try catch

The difference is explained on the page for Exception.
The Exception statement catches runtime exceptions that occur anywhere within the method or event. In most cases using a Try...Catch is better since it allows you to catch exceptions in specific parts of your code.

The use of the IOException or RuntimeException keywords just has to do with what “type” of error is being thrown - which can be used with either the Exception only approach or with a Try...Catch statement.

The https://documentation.xojo.com/api/language/runtime.htmlException page has a complete list of the types of errors that can be raised.

I hope that helps.

Also see: https://documentation.xojo.com/getting_started/debugging/exception_handling.html