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.
Try ... Catch ... Finally
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: Try ... Catch ... Finally
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.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.
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: Try ... Catch ... Finally
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.
Incidentally, I'm not suggesting that the existing error functions be removed.
Re: Try ... Catch ... Finally
i'm not familiar with macros but how about this?
(but i also prefer a native solution)
Greetings ... Peter
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
Greetings ... Peter
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: Try ... Catch ... Finally
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
Agreed though, a native implementation would be better. I bet Fred just loves getting all these feature requests
Re: Try ... Catch ... Finally
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.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.
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.
- SparrowhawkMMU
- Posts: 281
- Joined: Wed Aug 19, 2015 3:02 pm
- Location: United Kingdom
Re: Try ... Catch ... Finally
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.