You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
' OnError.bas
'
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
LOCAL MyERR AS LONG
LOCAL MyStr AS STRING
TRY
OPEN "OnError.txt" FOR INPUT ACCESS READ AS #1
LINE INPUT #1, MyStr
MSGBOX MyStr
CLOSE #1
CATCH
MyERR = ERRCLEAR
MSGBOX "Caught error: " & STR$(MyERR) & $CRLF & ERROR$(MyERR)
FINALLY
MSGBOX "End of program"
END TRY
END FUNCTION
Last edited by Robert DeBolt; 25 Feb 2008, 07:14 PM.
Cliff, Dunno if this is what you're looking for but here's a Macro I have found very handy:
Code:
'
Macro m_Err_Msg
If Err Then
Stile = %MB_SYSTEMMODAL Or _
%MB_ICONERROR Or _
%MB_ICONWARNING
? m$ & $CrLf & $CrLf & _
m1$ & $CrLf & $CrLf & _
Using$("# ", Err) & Error$(Err),_
Stile, _
" In " & FuncName$
ErrClear
End If
Reset m$, m1$
End Macro
'
Obviously have to Declare m$ & m1$ for custom msgs. Also ditto Stile (or just skip it using it and put the Styles in the msgbox. Double ditto m$'s)
=============================
O white and midnight sky,
O starry bath,
Wash me in thy pure,
heavenly crystal flood:
Cleanse me, ye stars,
from earthly soil and scath—
Let not one taint remain
in spirit or blood!
Richard Watson Gilder
=============================
Sorry guys, I wasted a ton of time and and instead should have just taken 42 seconds to do a simple test.
Code:
#COMPILE EXE
#DIM ALL
#INCLUDE "Win32Api.inc"
FUNCTION PBMAIN () AS LONG
LOCAL MyError AS DWORD
ON MyError GOTO ErrHandler
MyError = %INVALID_HANDLE_VALUE 'Doesn't do it
SetLastError MyError 'Nope still doesn't do it
MSGBOX "Answer found, you cant do a a custom ON ERROR concept"
EXIT FUNCTION
ErrHandler:
MSGBOX "Error Raised"
END FUNCTION
Answer found
I will chalk it up to "Programmer stress to get everything done, and not enough time to do half of it."
Makes perfect sense, but maybe I am not explaining correctly.
ERROR must be defined as some value
ERR must be defined as some value
I was looking at API functions that either return a handle, or 0 if error (compiler does not catch these because technically it is not an error per say (and I think I know why) but I was hoping to use built in functions of the compiler in functions that I can expect a value that is an error, but NOT an error to the compiler, just my making mistakes.
stuff like
ON 0 GOTO ErrHandler
could be done using macros to check the reply of each api call, but wanted to ask if already proven you can not ON 0 GOTO ErrHandler
but can macro to do the same?
I guess my only real answer is...."Try it" if you like it, then "Test it", and try to share what I learned for others that may wonder the same thing in the future.
Funny how the "ON ERROR" replies, I was looking at "ON 42" type for errors not caught at runtime
I like Bob's post of how the performance hit is not much at all (as is with most performance hit questions, but I am not worried about that at the moment, just if I can do it?)
MCM's MACRO idea I started implementing a while back while developing my latest-greatest, just so I could see how much time saving it would give me to not add multiple lines of error-checking, and use 1 macro (saves on file size a bit, but considering the size, the biggest time saver is to add 1 or 2 lines of code to each function vs 12 or more, on the "OFF-CHANCE" an error occurs that I had not thought of when I coded it, but a user may have caused it, so I can check a log and see as close as I can what may have caused it at that moment in time)
From time to time I get an error not caught until I can replicate it, (and do NOT give me the ole "Before you can code it, how can you break it?" train of thought because I may get lazy from time to time, or just not fully understand the concept, but I am always willing to go back and see if I can break my code)
I can easily use "ON ERROR" but for those lil things that slip through, can I use "ON MYERROR"?
(Its a matter of Symantecs at this point, but thought I would ask)
There is no real performance hit from ON ERROR GOTO. The executable will be a bit larger, but it's surely not significant. Highly recommended where appropriate.
FWIW, you can force your code to branch to an error handler based on application conditions....
Code:
%MYERROR = (any value between 151 and 240)
ON ERROR GOTO MyErrorTrap
yadda, yadda
IF Condition THEN
ERROR %MYERROR
.....
MyErrorTrap:
WhatEver
RESUME somewhere
That's a pretty neat way to handle errors. Is there a performance hit for using ON ERROR GOTO? The documentation didn't mention any.
%MyERROR = 42 'And yes I chose that value on purpose if you get the joke
ON %MyERROR GOTO <Insert Error Handler>
In a few (not many) but few cases, it would be nice to build a stronger error handling routine geared more toward the purpose of the program rather than the cryptic answers the user would get.
For that matter, can I also use
Code:
%MyERROR = 42 'And yes I chose that value on purpose if you get the joke
ON ERROR GOTO <Insert Error Handler>
ON %MyERROR GOTO <Insert Error Handler>
in combination? (I suppose I could try and work up an example test using)
Code:
ON ERROR GOTO <Insert Error Handler>
ON ERR GOTO <Insert Error Handler>
and see if that works as well for a beginning test
FWIW, you can force your code to branch to an error handler based on application conditions....
Code:
%MYERROR = (any value between 151 and 240)
ON ERROR GOTO MyErrorTrap
yadda, yadda
IF Condition THEN
ERROR %MYERROR
.....
MyErrorTrap:
WhatEver
RESUME somewhere
I "think" you can also use ERROR to set the system ERR variable if you are checking ERR in-line rather than using ON ERROR GOTO. I never tried this myself, but it just might work.
You're right Michael.
I was concentrating on the ON x GOSUB and didn't even look at the ON ERROR syntax or think about it much.
The point is pretty much the same though with a little adjustment if that is the direction he wants to go. As long as he takes care with the placement of the label (without the RETURN I associated with the goto label).
Paul is right and you can access the ON GOSUB/GOTO with the following, depending on how many errors you're planning on making.
Code:
ON ERROR GOSUB uhoh
....
your error filled code here
...
ON myerror GOSUB badmove, worsemove, ...
uhoh:
if err=53 then
myerror =1
elseif err=72 then
myerror =2
...
end if
RETURN
badmove:
blah, blah, etc
RETURN
worsemove:
&*^%% @@
RETURN
Of course, the subroutines could be accessed straight from the IF block.
but wondered if PB allows a "ON <Insert Value> GOTO/GOSUB"?
PB has ON..GOTO and ON..GOSUB statements which, as far as I know, do the same thing those statements did on DOS BASICs.
ON ERROR is a different type of command as the jump to the specified error handler takes place when the error occurs and not when the ON ERROR statement is encountered.
This is theoretical but fairly certain this is not implemented in PB, (and most likely other languages) just because the scope of the question.
But is it possible to do an "ON ERROR GOTO/GOSUB etc" do something?
Like I could "ON MYERROR GOTO/GOSUB"??
I understand the differences between SDK and DDT and how the compiler handles things (more or less), and how GetLastError and ERR work (for the most part), but wondered if PB allows a "ON <Insert Value> GOTO/GOSUB"?
This way in small niches I could handle my own error handling routines? (Sometimes I find documentation incorrect, or ALMOST correct, but not quite, that maybe if I could catch it, I could correct it?)
If not, then curious how "ON ERROR" works exactly? (I would assume kinda like macro's work, and "ON ERROR" is a compiler replacement that checks if there is a runtime error or not on each line of code?)
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Leave a comment: