Handling Try/Catch

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
SergeyA
New User
New User
Posts: 8
Joined: Sun Jan 20, 2013 9:09 am

Handling Try/Catch

Post by SergeyA »

Hello, please add support for handling exceptions. Try/Catch

Image
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Handling Try/Catch

Post by IdeasVacuum »

Encourages the use of a sloppy coding style :?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Handling Try/Catch

Post by ts-soft »

+1
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.
Image
Manuel
User
User
Posts: 14
Joined: Thu Mar 02, 2017 11:15 pm
Location: Barcelona

Re: Handling Try/Catch

Post by Manuel »

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
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Handling Try/Catch

Post by RSBasic »

+1
Please Catch with Exceptions.
Image
Image
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Handling Try/Catch

Post by wilbert »

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
PokeL has no return value so the second approach makes no sense.
Try / Catch is useful in a lot of cases for example when working with external libraries.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Handling Try/Catch

Post by Mijikai »

+1
would be great to have
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Handling Try/Catch

Post by Shield »

Manuel wrote:Can someone explain the practical difference between these two sets of codes?
While I am certain that exceptions never make it into PB, here are some advantages exceptions would offer:
  • 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).
Image
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
SergeyA
New User
New User
Posts: 8
Joined: Sun Jan 20, 2013 9:09 am

Re: Handling Try/Catch

Post by SergeyA »

This option does not work, the application is shutting down.

Enable debugger:
Image

Disable debugger:
Image

Reference:
Image
Manuel
User
User
Posts: 14
Joined: Thu Mar 02, 2017 11:15 pm
Location: Barcelona

Re: Handling Try/Catch

Post by Manuel »

wilbert wrote:PokeL has no return value so the second approach makes no sense.
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
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Handling Try/Catch

Post by wilbert »

Manuel wrote:I should amend my question to be: how is Try/Catch better in those cases than If/EndIf?
It is not useful in case of LoadImage since LoadImage doesn't throw an error but returns zero when it fails.
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)
User avatar
chi
Addict
Addict
Posts: 1034
Joined: Sat May 05, 2007 5:31 pm
Location: Linz, Austria

Re: Handling Try/Catch

Post by chi »

+1
Et cetera is my worst enemy
Manuel
User
User
Posts: 14
Joined: Thu Mar 02, 2017 11:15 pm
Location: Barcelona

Re: Handling Try/Catch

Post by Manuel »

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.
So it's like the "Trap" command of some older Basics, or "On Error Resume Next" of Visual Basic Classic?
SergeyA
New User
New User
Posts: 8
Joined: Sun Jan 20, 2013 9:09 am

Re: Handling Try/Catch

Post by SergeyA »

Try/Catch allows you to capture application errors without affecting the application, you can collect error data and issue corrected updates for your clients.
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: Handling Try/Catch

Post by DarkDragon »

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
Post Reply