Handling Try/Catch
-
- Always Here
- Posts: 6425
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Handling Try/Catch
Encourages the use of a sloppy coding style
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Handling Try/Catch
+1
for try / catch
for try / catch
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Re: Handling Try/Catch
Can someone explain the practical difference between these two sets of codes? Because I fail to see why Try/Catch is needed, especially when it adds more typing and code.
Code: Select all
Try
PokeL(0, 12345)
Catch
ErrorLog("Write to zero address")
EndTry
Code: Select all
If PokeL(0, 12345) = 0 ; Failure.
ErrorLog("Write to zero address")
EndIf
Re: Handling Try/Catch
PokeL has no return value so the second approach makes no sense.Manuel wrote:Can someone explain the practical difference between these two sets of codes? Because I fail to see why Try/Catch is needed, especially when it adds more typing and code.
Code: Select all
Try PokeL(0, 12345) Catch ErrorLog("Write to zero address") EndTry
Code: Select all
If PokeL(0, 12345) = 0 ; Failure. ErrorLog("Write to zero address") EndIf
Try / Catch is useful in a lot of cases for example when working with external libraries.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Handling Try/Catch
+1
would be great to have
would be great to have
Re: Handling Try/Catch
While I am certain that exceptions never make it into PB, here are some advantages exceptions would offer:Manuel wrote:Can someone explain the practical difference between these two sets of codes?
- Actually less typing because not every function has to be checked for errors (i.e. they can be grouped).
- It is possible to differentiate between different error types (groups).
- Exceptions can bubble up the call stack.
- Exceptions can be handled where appropriate instead of directly at the call site.
- Exceptions must be handled. If not handled, the program will ultimately abort. While this may sound drastic,
it is actually a much better option than silently ignore an error that may crash the program later anyways. - Better separation of code and error handling.
- Arguably better performance if no exceptions occur (exceptions can be implemented with zero runtime overhead).
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
Re: Handling Try/Catch
This option does not work, the application is shutting down.
Enable debugger:
Disable debugger:
Reference:
Enable debugger:
Disable debugger:
Reference:
Re: Handling Try/Catch
True, so that was a bad example. I've seen other Try/Catch examples where the tested item does have a return value (such as "Try LoadImage..."), so I should amend my question to be: how is Try/Catch better in those cases than If/EndIf?wilbert wrote:PokeL has no return value so the second approach makes no sense.
Re: Handling Try/Catch
It is not useful in case of LoadImage since LoadImage doesn't throw an error but returns zero when it fails.Manuel wrote:I should amend my question to be: how is Try/Catch better in those cases than If/EndIf?
Try/Catch is useful for those situations where an error can occur that isn't handled by PureBasic so the application doesn't crash.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Handling Try/Catch
So it's like the "Trap" command of some older Basics, or "On Error Resume Next" of Visual Basic Classic?wilbert wrote:Try/Catch is useful for those situations where an error can occur that isn't handled by PureBasic so the application doesn't crash.
Re: Handling Try/Catch
Try/Catch allows you to capture application errors without affecting the application, you can collect error data and issue corrected updates for your clients.
-
- Addict
- Posts: 2228
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Re: Handling Try/Catch
I'd especially need a Finally clause for cleaning up when the procedure ends in cases where many procedurereturns exist. This is very useful.
bye,
Daniel
Daniel