Page 1 of 1

Try ... Catch ... Finally

Posted: Wed Mar 23, 2016 2:43 pm
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.

Re: Try ... Catch ... Finally

Posted: Thu Mar 24, 2016 1:45 am
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.

Re: Try ... Catch ... Finally

Posted: Thu Mar 24, 2016 9:16 am
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.

Re: Try ... Catch ... Finally

Posted: Thu Mar 24, 2016 9:42 am
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

Re: Try ... Catch ... Finally

Posted: Thu Mar 24, 2016 10:13 am
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 ;)

Re: Try ... Catch ... Finally

Posted: Thu Mar 24, 2016 11:44 pm
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.

Re: Try ... Catch ... Finally

Posted: Fri Mar 25, 2016 11:22 am
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.