Try ... Catch ... Finally

Got an idea for enhancing SpiderBasic? New command(s) you'd like to see?
User avatar
SparrowhawkMMU
Posts: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Try ... Catch ... Finally

Post by SparrowhawkMMU »

Hi,

Is there any chance that SpiderBasic could support Try-Catch-Finally blocks with Throw(ing) of errors, seeing that JS supports them natively?

It would make the control flow of programs much, much easier and make code more robust too.
tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Try ... Catch ... Finally

Post by tj1010 »

SparrowhawkMMU wrote:Hi,

Is there any chance that SpiderBasic could support Try-Catch-Finally blocks with Throw(ing) of errors, seeing that JS supports them natively?

It would make the control flow of programs much, much easier and make code more robust too.
I use them in JS dev a lot. I don't see where you'd use them in SB. The JSON library is the most likely to need it and it already has returns for stuff like isJSON(), getJSON*(). Checking returns is less bytes&work.
User avatar
SparrowhawkMMU
Posts: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Try ... Catch ... Finally

Post by SparrowhawkMMU »

Try Catch in my mind is often (not always) more elegant than constantly checking return codes and having deeply nested if statements. The ability to throw errors to the catch block makes code readability much clearer (again, my opinion)

Incidentally, I'm not suggesting that the existing error functions be removed.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Try ... Catch ... Finally

Post by Peter »

i'm not familiar with macros but how about this?

Code: Select all

Macro SbTry
  ! try {
EndMacro
Macro SbCatch
  ! } catch(v_err) {
EndMacro
Macro SbFinally
  ! } finally {
EndMacro
Macro SbEndTry
  ! }
EndMacro

; Test

Procedure ThrowSomeError()
  ! throw("some error");
EndProcedure

Define err

SbTry
  ThrowSomeError()
SbCatch
  Debug err
SbFinally
  Debug "Finally..."
SbEndTry
(but i also prefer a native solution)

Greetings ... Peter
User avatar
SparrowhawkMMU
Posts: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Try ... Catch ... Finally

Post by SparrowhawkMMU »

I had not considered macros - clever idea Peter :)

Agreed though, a native implementation would be better. I bet Fred just loves getting all these feature requests ;)
tj1010
Posts: 201
Joined: Wed May 27, 2015 1:36 pm
Contact:

Re: Try ... Catch ... Finally

Post by tj1010 »

SparrowhawkMMU wrote:Try Catch in my mind is often (not always) more elegant than constantly checking return codes and having deeply nested if statements. The ability to throw errors to the catch block makes code readability much clearer (again, my opinion)

Incidentally, I'm not suggesting that the existing error functions be removed.
Then you just put the IF or Switch branches in the exception branch unless you just dump raw errors to your users and crash your app. Try:catch saves no coding really unless you pass on a lot of problems to the end-user.

I wish this wasn't the case though cause nothing if more annoying than ten if:else:endif branches in 600 lines of code for things like database and input handling.
User avatar
SparrowhawkMMU
Posts: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Try ... Catch ... Finally

Post by SparrowhawkMMU »

It's not about saving coding, it's about where the error is handled. I'd rather my error handling were mostly outside the business logic.
Post Reply