Try/Catch/Finally error handling...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Try/Catch/Finally error handling...

Post by Dawgmann »

The following is an example of an ideal Try/Catch code block. Because I am just now learning PB, I don't know if it will work well with this language, but in other languages it allows for some spectacular error handling. Combined with events, it also allows the coder to decide where and the best manner in which to deal with the error.

Example:

Procedure MyProcedure(Param1, Param2)
    Protected.i intErr

    Try ;attempt the following code...
        DoSomething() ;normal code here...
        DoSomethingElse()

        intErr = #False ;indicate no error occurred...
    Catch ;handle any raised errors...
        If ErrorNumber <> -1 ;if any error other than successful execution is returned...
            PostEvent(#EventRaiseError) ;trigger the RaiseError event to allow coder to decided the best place and manner to deal with the error.
        EndIf

        intErr = #True ;indicate an error occurred...
    Finally ;(This segment would be optional for the coder to use) execute the following code no matter what so coder can decide how to best proceed with program execution or shut down...
        CloseDocument() ;safely close document/encrypted data file to prevent data loss and corruption...

        If intErr = #False ;if no error occurred...
            ProcedureReturn ThisValue ;return some value determined by program execution...
        Else ;if an error occurred...
            ProcedureReturn ThatValue ;return a different value determined by the type of error...
        EndIf
    EndTry
EndProcedure
Last edited by Dawgmann on Tue Sep 10, 2019 7:01 am, edited 1 time in total.
Cyllceaux
Enthusiast
Enthusiast
Posts: 363
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Try/Catch/Finally error handling...

Post by Cyllceaux »

Why?

The first thing I learned in Java was: Nothing is more expensive than try/catch and a Null-Check.

You can check every variable and pointer on 0. If there is an IMA, than someone miss this check, miss filling the memory or an memory error.

When you write clean code, you don't need the communication over Exceptions.
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

Why? Because the encrypted data of my clients is worth more than your opinion.
If you don't like a feature or other people's method of programming - don't use it.
And given Java's history of bugginess, along with the spaghetti code development style of a large portion of the hipsters who use it, Java isn't exactly the best example to bring up when trying to persuade someone that extensive handling methods aren't necessary.
But hey... as long as Java is used, IT workers continually having to fix crashed systems will always have job security. Just ask Amazon...
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Try/Catch/Finally error handling...

Post by collectordave »

Just a quick attempt:-

Code: Select all

MessageRequester("OnError test", "Test start")

  Try:
  OnErrorGoto(?Catch)
  PokeS(10, "Hello World") ; Cause a #PB_OnError_InvalidMemory error
  ;Comment out above line to see no error i.e. Miss out catch code
  Goto Finally
  
  MessageRequester("OnError test", "This should never be displayed")
  
  Catch:
  MessageRequester("OnError test", "The following error happened: " + ErrorMessage())

  ;Everything Checked and Safe Depends on error!
  
 Goto Finally
  
  End
  
  Finally:
  
  Debug "Finalising things"
Maybe what you are looking for?

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

@collectordave

That is great work, and I appreciate it. So, thank you very much for your help!!

Unfortunately, however, when an error is raised in a PB procedure, something prohibits it from returning a value - the OnErrorResume() command was deprecated at some point. When working with enterprise-level data objects, the ability for a function to return a value even in the event of an error is EXTREMELY crucial, which is why it is implemented in enterprise-level languages. As Cyllceaux mentioned, the Try/Catch/Finally block is a bit expensive on system resources and program execution speed. However, with data-critical apps, well, the stability and security of the data is worth more than the speed.

One reason I have really been looking into PB is because of the blinding speed of its compiled executables. Most languages which use Try/Catch/Finally are either bytecode (Java) or common intermediate language (C#/CIL - Microsoft's version of bytecode which is actually based on Turbo Pascal/Delphi by their creator Anders Hejlsberg) executing via a virtual machine, so they're just naturally slow at execution to begin with. If PB had Try/Catch/Finally, any programs compiled using it - even on a bad day - would still blow away a C# or Java app on their best days.

If PB functions could return a value following an error, then your example would work great for me.
collectordave
Addict
Addict
Posts: 1275
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Try/Catch/Finally error handling...

Post by collectordave »

Errors caught by OnError are almost always fatal so quitting is the only option. Check the on error documentation.

However you can raise your own errors and deal with them.

Try This done for mac as on windows you can use raiseerror()

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

Everyone other than myself seems to keep missing the point.
I know most run-time errors are fatal and the app needs to shutdown. However... and this is the main point... the function in which the error occurs still MUST return some value to its calling function due to the type of data objects I'm connected to, after which point I can shut down the app.

It's not that I just want this ability. It's a non-negotiable requirement for my work, which is why I'm more than likely going to have to go with ObjectPascal/Delphi which I'm familiar with and it is already well equipped for enterprise development.

Because of its speed and lack of bloat, PureBasic has the opportunity to be a goldmine with 3 or 4 simple added features whereby it would take up the mind-share of VB6 development, as well as stealing away quite a bit of VB.NET/C# development. But I guess Fred has his reason for not wanting to go there.
User avatar
skywalk
Addict
Addict
Posts: 3562
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Try/Catch/Finally error handling...

Post by skywalk »

I am surprised you did not search "TRY Catch" many people suggested :?:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Marc56us
Addict
Addict
Posts: 1130
Joined: Sat Feb 08, 2014 3:26 pm
Location: France

Re: Try/Catch/Finally error handling...

Post by Marc56us »

If PB is fast and light, it is precisely because he doesn't bother with all these checks.

There are only two public for PB
  • Beginners (who do not handle user errors)
  • Experienced (sometime professionals) who know how to handle errors without "trial and error" (try/catch)
The "On Error" lib and the IsXXX(1) functions are sufficient to make stable programs. All native PB functions return a value.

And we've been doing this for over 20 years in PB :wink:

(1) IsBillboardGroup, IsCamera, IsDatabase, IsDialog, IsDirectory, IsEffect, IsEntity, IsFile, IsFingerprint, IsFont, IsFtp, IsGadget, IsGadget3D, IsImage, IsInfinity, IsInsidePath, IsInsideStroke, IsJSON, IsJoint, IsLibrary, IsLight, IsMail, IsMaterial, IsMenu, IsMesh, IsMovie, IsMusic, IsNaN, IsNode, IsParticleEmitter, IsPathEmpty, IsProgram, IsRegularExpression, IsRuntime, IsScreenActive, IsSerialPort, IsSound, IsSound3D, IsSprite, IsStaticGeometry, IsStatusBar, IsSysTrayIcon, IsText3D, IsTexture, IsThread, IsToolBar, IsWindow, IsWindow3D, IsXML
Last edited by Marc56us on Tue Sep 10, 2019 4:27 pm, edited 1 time in total.
(English is not my native language)
PureBasic Latest version, Windows 10 family x64, Linux Debian & Slackware (VirtualBox & WSL 1) Xfce, Raspberry Raspbian
Intel i7-8700 3.20GHz, RAM 48 Nvidia GTX 1060 3GB, Monitor Asus VP348 3440 x 1440, 100%
Everything
Enthusiast
Enthusiast
Posts: 210
Joined: Sat Jul 07, 2018 6:50 pm

Re: Try/Catch/Finally error handling...

Post by Everything »

skywalk wrote:I am surprised you did not search "TRY Catch" many people suggested
Agree, for Windows it's valid native way and also flexible one.
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

Foot Doctor: "You brain surgeons don't really need such and such tool to perform your surgery."

Brain Surgeon: "How do you know we don't need such and such tool to perform our work. Do you perform surgery? And do you perform the same type of surgery we do?"

Foot Doctor: "No, not at all. And that's precisely how I know you don't need such and such tool, because we don't use it. I'm qualified to say what you do or don't need based on what I do because we're both physicians, right?"
Little John
Addict
Addict
Posts: 4020
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Try/Catch/Finally error handling...

Post by Little John »

Dawgmann wrote:Foot Doctor: "You brain surgeons don't really need such and such tool to perform your surgery."

Brain Surgeon: "How do you know we don't need such and such tool to perform our work. Do you perform surgery? And do you perform the same type of surgery we do?"

Foot Doctor: "No, not at all. And that's precisely how I know you don't need such and such tool, because we don't use it. I'm qualified to say what you do or don't need based on what I do because we're both physicians, right?"
In the beginning of this thread, it looked as if you wanted to start a serious discussion ...
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

@skywalk
skywalk wrote:I am surprised you did not search "TRY Catch" many people suggested :?:
I have attempted to use many Try/Catch macro samples from the forum, but none have worked so far because they still did not allow the function to return a value if an error occurred. This is my first time seeing Danilo's version. I just tested it a few times and it does allow my functions to return a value, but ErrorCode() always returns a -1 (no error) when using it. I still need the error code and error description in addition the ability to return a value in the event of an error. So, unfortunately, it's no good to me either.
Last edited by Dawgmann on Wed Sep 11, 2019 4:06 am, edited 2 times in total.
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

@Marc56us

"The "On Error" lib and the IsXXX(1) functions are sufficient to make stable programs. All native PB functions return a value."

Using all the example I could find using PB's 'OnErr' library, I could not get my functions to return a value after an error was raised without a Resume command. Maybe I downloaded a different version of PB than everyone else, but I doubt it.

Anyway, skywalker pointed me to Danilo's version of the Try/Catch and it works great.
Dawgmann
User
User
Posts: 40
Joined: Wed Jul 17, 2019 5:32 pm

Re: Try/Catch/Finally error handling...

Post by Dawgmann »

@ Little John
Little John wrote:In the beginning of this thread, it looked as if you wanted to start a serious discussion ...
It is a serious discussion. It's just that as is often the case in programming forums, other programmers were more interested in flexing their fragile egos by telling me what they thought was the right way to do things rather than helping me figure out a way to meet my requirements.

skywalker helped meet achieve my goals, this particular one for which there is no negotiation. It is either find a way to meet this requirement in PB, or throw PB out the window and go on to another language.
Last edited by Dawgmann on Tue Sep 10, 2019 7:00 pm, edited 1 time in total.
Post Reply