curly
Full Member
Posts: 161
|
Post by curly on Mar 20, 2021 12:58:39 GMT -5
I've had a good look through the help files but either can't find what I need or it 'aint there?
Many years ago when I used TurboBasic V1, I half remember using a statement something like, 'on error goto wherever', and at wherever, if error code is 111, then etc
Does LB have similar error handling? The people who will use my programme will try to break it without knowing, and I need to be able to handle it.
|
|
|
Post by tsh73 on Mar 20, 2021 13:14:26 GMT -5
www.libertybasicuniversity.com/lb4help/ON_ERROR.htmYou better check if error you trying to catch could be caught at all also, there is no "RESUME NEXT" of other BASIC have ("I had error - I caught it - now I just skip that statement"). and ON ERROR could not be turned off So usual way is to wrap thing that could fail into SUB or FUNCTION and turn ON ERROR just there.
|
|
|
Post by Brandon Parker on Mar 20, 2021 21:11:41 GMT -5
You can "fix" the issue that triggered the error and then use the "Resume" command to send the program flow back to the line that caused the error...
It's not "Resume Next" for sure, but IMO that could be a disastrous command to be implemented in any language. Handling the exception/error and re-executing the offending line should be enough for most requirements.
Print test(0)
Function test(val) On Error GoTo [Error] minValue = 1 test = 5/val Exit Function [Error] Print Err;" : ";Err$ val = minValue Resume End Function
{:0)
Brandon Parker
|
|
|
Post by Carl Gundel on Mar 20, 2021 21:58:39 GMT -5
You can "fix" the issue that triggered the error and then use the "Resume" command to send the program flow back to the line that caused the error... It's not "Resume Next" for sure, but IMO that could be a disastrous command to be implemented in any language. Handling the exception/error and re-executing the offending line should be enough for most requirements. Print test(0)
Function test(val) On Error GoTo [Error] minValue = 1 test = 5/val Exit Function [Error] Print Err;" : ";Err$ val = minValue Resume End Function {:0) Brandon Parker I agree that I never really understood what the appeal of RESUME NEXT is. Even RESUME seems slightly unsettling to me. Better to trap the error and then deliberately go back and retry without using RESUME at all when you can.
|
|
|
Post by tsh73 on Mar 20, 2021 23:09:43 GMT -5
It's not "Resume Next" for sure, but IMO that could be a disastrous command to be implemented in any language. Brandon Parker Visual Basic programmers look at you in disbelief.
|
|
|
Post by tsh73 on Mar 20, 2021 23:14:43 GMT -5
≥> Better to trap the error and then deliberately go back and retry without using RESUME at all when you can.
Please give some code. How else one supposed to "go back" from error handling if not by RESUME?
|
|
|
Post by Rod on Mar 21, 2021 4:19:13 GMT -5
I have only ever had success with on error when handling devices. Open com etc.
The on error command needs to be specific. A general on error issued at the head of the program isn’t much use since you will be unlikely to know what to correct.
So it works best just ahead of a command that might fail.
But the best protection is debugging and range or limit protection built into the code.
If Curly could list a few of the errors he has encountered we could show how best to avoid or manage them.
Divide by zero is the most obvious error to manage.
|
|
curly
Full Member
Posts: 161
|
Post by curly on Mar 21, 2021 8:23:55 GMT -5
My current main concern is where input is required to a text box, and someone tries to enter an infinite number of characters? Every time I make a change, I rename my LB fie, and then try to break it. So, for example, can the maximum number of characters be set when creating the text box?
So, if I understand correctly, I can use an 'on error goto [wherever]' statement immediately after the user is asked to do something, and err$ will have a value that indicates what has gone wrong, such as file not present, file already open, stack overflow etc. If that is the case, is there a list of error codes please?
|
|
|
Post by tsh73 on Mar 21, 2021 8:57:20 GMT -5
It could be done libertybasiccom.proboards.com/thread/811/File not present generally checked by FileExist function from the Help file www.libertybasicuniversity.com/lb4help/38L5ZGN.htmAs for exhaustive list of error codes I doubt it is possible at all. About StackOverflow: 10 gosub 10 which supposed to lead to stack overflow ended with Err 0 Err$ system primitive failed But call to recursive function function test() y = test() end function
just ended with BASIC crash after error As I've said, not all errors are caught. (again I do not have a list)
|
|
|
Post by mknarr on Mar 21, 2021 10:20:58 GMT -5
Here is an error handling routine I use.
[error] 'Error trapping. b$=" and the program will end." select case Err case 0 text$="An undefined error has occurred" case 3 text$="Return without GOSUB" case 4 text$="Read past end of data" case 8 text$="Branch Label not found" case 9 text$="Subscript out of range" case 11 text$="A division by zero has occurred" case 52 text$="Error opening file" case 53 text$="Can not find the file specified" case 55 text$="Error opening file" case 58 text$="Cannot create a file when that file already exists" case 62 text$="Input past the end of file" end select notice "Fatal Error!"+chr$(13)+text$+b$ end
I also seem to remember that I have another program where depending on the version, an error can occur when the program opens the ini file in a sub and there are not enough entries. The error handling program there fixes the ini file and then continues with the program, goes back and then it sees:
on error goto [error] So I believe you can have more then one error trap but the program only uses the last one it sees.
|
|
|
Post by tsh73 on Mar 21, 2021 10:58:57 GMT -5
Thanks for the list. as for
it likely would be this one
62 Input past end of file: #1
Also, code 0 could have descriptive text I already seen
0 system primitive failed (then recursive GOSUB with no end) 0 OS Error: System cannot find the file specified. (then no file exist for OPEN) 0 Handle #1 already in use (then file is already open) 0 bad file handle: #1 (then file was not open but I trued to CLOSE it)
|
|
|
Post by alincon on Mar 21, 2021 11:57:49 GMT -5
To limit characters in a text box:
call tbLimit hwnd(#payer.headSocSec),11
sub tbLimit htext,hlmt calldll #user32, "SendMessageA",htext as word,_EM_LIMITTEXT as word, _ hlmt as word, 0 as long, r as long end sub
|
|
|
Post by Rod on Mar 21, 2021 13:01:41 GMT -5
It's the "fatal" bit that makes me feel it isn't worth much coding effort. But You guys have put more effort in than me. Prevention remains the best cure.
User input being an example. Don't read more than the number of characters you expect, (they can key as many as they like, but good solution from Alinco.) don't allow values outwith parameters. Don't move on till they are. Give as much feedback as you can to correct stupidity.
Also use controls to limit choices, combobox, spinners, checkbox etc all control choice.
|
|