Error Handling Inside Function

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
SlowCoder
Coder
Posts: 26
Joined: Wed Oct 01, 2008 8:27 am

Error Handling Inside Function

Post by SlowCoder »

Hi all! I'm new here, but did lots of QB programming back in the early 90's.

I previously posted this on another board (VBForums), but have not had any responses in 2 weeks. So now I've come to you!

I want to determine the maximum number of lines my display can have, so I wrote the following function:

Code: Select all

FUNCTION HighScreenRows
   DIM ScreenMode(4)
   '*** find maximum screen rows ***
   ScreenMode(1) = 25: ScreenMode(2) = 43: ScreenMode(3) = 50: ScreenMode(4) = 60
   FOR intRows = 1 TO 4
      ResolutionGood = 1
      ON ERROR GOTO ErrorHandler
      WIDTH 80, ScreenMode(intRows)
      ON ERROR GOTO 0
      '
      IF ResolutionGood = 1 THEN HighScreenRows = ScreenMode(intRows)
   NEXT intRows
   '
   VIEW PRINT
   EXIT FUNCTION

ErrorHandler:
   '*** capture screenmode errors ***
   ResolutionGood = 0
   RESUME NEXT

END FUNCTION
Unfortunately this isn't working, as it doesn't like the error handling. I get a "label not found" error on my "on error goto ErrorHandler" line.

So, what I'd like to know is a couple of things:
1. How do I properly handle my errors in subs/functions?
2. Is there a better way to determine the highest rows available?

I'm using QuickBasic 4.5, not QBasic.

Thanks, guys!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

You cannot use ON ERROR in a SUB or Function. You don't need it there.

Place ON ERROR ahead of the FUNCTION or SUB calls in the main program. It will still work from the main module.

Also move your ErrorHandler sub to the main module! That is where ON ERROR looks for it.

Be careful with error handlers when creating a program. ANY error will trip it and you will not be able to know what the error was. You can use ERR to read the error numbers and categorize the error in the Handler. I generally disable my error handler when adding code. Just comment out the ON ERROR line. Otherwise you may have problems later!

Ted
Last edited by burger2227 on Wed Oct 01, 2008 1:03 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
SlowCoder
Coder
Posts: 26
Joined: Wed Oct 01, 2008 8:27 am

Post by SlowCoder »

Hmmm ... I understand what you're saying, and that explains a lot actually.

That leaves me with an unclean feeling. I try to keep the cleanest code possible. I would like to keep as much of the code within the function. Having to place error catching routines outside just feels dirty.

So, I'll reiterate my 2nd question. Is there a way to determine the maximum number of lines a different way?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

YOU must learn the screen resolutions for Text and Graphics. Screen 0 has several WIDTH options for text. They are listed in the SCREEN Help section. The Text character sizes are also listed.

I saw an Interrupt routine that returned those values, but it messed up my Monitor settings. Here are some PEEK functions I found:

Code: Select all

DEF SEG = 0
PRINT PEEK(&H449)   '     1 byte, current video mode
PRINT PEEK(&H44A)   '    2 bytes, current screen width (columns)
PRINT PEEK(&H484)   '    1 byte, EGA/VGA screen height (rows)
DEF SEG
I have a zip file with 2 PEEK and POKE listings at QbasicStation.com in Member Files if you want to play around with settings. BE CAREFUL and use DEF SEG!

Ted
Last edited by burger2227 on Wed Oct 01, 2008 3:05 pm, edited 1 time in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
SlowCoder
Coder
Posts: 26
Joined: Wed Oct 01, 2008 8:27 am

Post by SlowCoder »

I know the text/graphic screen modes. Back in my school days I wrote a number of graphical applications, when EGA was the babe of babes. :) That is not the issue here. What I am try to accomplish (which may be accomplished by your example) is how to determine, on the fly, the number of text lines a video card can handle. I am using screen 0 for this.

The reason for this ... I am writing a menuing system that will, with luck, be used by a wide audience, many of which may have legacy hardware for the purpose. This could even include old CGA video cards and monitors.
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

Interesting :)...I'm gonna way a look at that :).

Burger, those peeks seem to work fine for me here...on a laptop ;)
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Good to know Myst. Sometimes it is hard to find things so old that STILL work LOL. PEEKPOKE.ZIP information file:

http://www.qbasicstation.com/index.php? ... &filecat=4

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Ted, I liked your basic program kernel very much, and added some code for myself, to go through all screen modes without having to do a manual change each time. I thought someone else might like to use it, so, here it is:

Code: Select all

GOTO RAE
'Program by "burger22", alias Ted, on 10/02/2008, as posted on:
'http://www.petesqbsite.com/forum/viewtopic.php?t=2773&sid=787edd50e4aa7d61b03a2f5ebd9a723f

DEF SEG = 0
PRINT PEEK(&H449)   '     1 byte, current video mode
PRINT PEEK(&H44A)   '    2 bytes, current screen width (columns)
PRINT PEEK(&H484)   '    1 byte, EGA/VGA screen height (rows)
DEF SEG

'''==========================================================================
RAE: 'Ted's program, as modifie by Ralph for all screen mode's in QuickBASIC
CLS
L = 0: H = 2
DO
  FOR s = L TO H
    SCREEN s
    PRINT " SCREEN"; s
    DEF SEG = 0
    sp = 6: IF s < 7 THEN sp = 7
    PRINT SPC(sp); PEEK(&H449); "Video mode"'      1 byte, current video mode
    PRINT "      "; PEEK(&H44A); "Columns (width)"' 2 bytes, current screen width (columns)
    PRINT "      "; PEEK(&H484); "Rows (height)"'   1 byte, EGA/VGA screen height (rows)
    DEF SEG
    PRINT
    PRINT "Press <Esc> to end, press any other key to continue"
    k$ = "": WHILE k$ = "": k$ = INKEY$: WEND
    IF k$ = CHR$(27) THEN SYSTEM
  NEXT s
  L = 7: H = 13
LOOP UNTIL s = 14

SYSTEM
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

COOL

Post by burger2227 »

Nice idea Ralph. I would add 1 to the row counts however.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Thanks, Ted

Post by Ralph »

Thanks to you, I have now improved the program a little, and corrected the total number of rows that your oritginal code provided, by adding 1, as you suggested. Here is the new code:

Code: Select all

GOTO RAE
'Program by "burger22", alias Ted, on 10/02/2008, as posted on:
'http://www.petesqbsite.com/forum/viewtopic.php?t=2773&sid=787edd50e4aa7d61b03a2f5ebd9a723f

DEF SEG = 0
PRINT PEEK(&H449)   '     1 byte, current video mode
PRINT PEEK(&H44A)   '    2 bytes, current screen width (columns)
PRINT PEEK(&H484)   '    1 byte, EGA/VGA screen height (rows)
DEF SEG

'''==========================================================================
RAE: 'Ted's program, as modifie by Ralph for all screen mode's in QuickBASIC

L = 0: H = 2

DO
  FOR s = L TO H
    SCREEN s
   

    'get data through PEEK
    DEF SEG = 0
    video = PEEK(&H449)    '1 byte,  current video mode
    columns = PEEK(&H44A)  '2 bytes, current screen width (columns)
    rows = PEEK(&H484) + 1 '1 byte,  current screen height (rows)
    DEF SEG
   

    CLS
    'in order to avoid pushing the screen up two spaces, it is necessary to
    'locate the bottom line here, before printing other data to the screen
    LOCATE rows, 1
    PRINT rows; "<Esc> to end, <Spacebar> to continue"
   
    'print screen data
    LOCATE 1, 5: PRINT "SCREEN";
    IF s <10> 6 THEN sp = 6
    LOCATE 2, sp: PRINT video; "is the video mode"
    LOCATE 3, 6: PRINT columns; "columns (width)"
    LOCATE 4, 6: PRINT rows; "total rows (height)"
    LOCATE rows - 2, 5: PRINT "= total rows-2 = normal useable rows"

    LOCATE 1, 1
    FOR I = 1 TO rows - 2
      PRINT I
    NEXT I
    
    k$ = "": WHILE k$ = "": k$ = INKEY$: WEND
    IF k$ = CHR$(27) THEN SYSTEM
  NEXT s
  L = 7: H = 13
LOOP UNTIL s = 14


SYSTEM
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

It also works with WIDTH column and row settings in various screen modes.

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Ted, I don't follow you. What works with WIDTH columns and rows, too?
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Screen 0 for example can use the following with WIDTH columns, rows.

Columns can be 40 or 80. Rows can be 25, 43 or 50.

So if you set WIDTH 40, 50 then it will return those values.

Try WIDTH 80, 60 in Screen 12.

The Help index also shows that Screen 9 and 10 can have columns of 80 only and 25 or 43 rows.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

I understand, now. Yes, but, with my eyesight getting worse by the day, I think I may be forced into using only SCREEN mode 13, width 40, height 25! Or, I might just change my present 19" monitor for a 24" one, if I can find one and can afford the price.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
SlowCoder
Coder
Posts: 26
Joined: Wed Oct 01, 2008 8:27 am

Post by SlowCoder »

I am glad that this subject has raised the interest of some ... Thank you.

However, I see that the peeks only return the current values, and not the maximum available.

I'm still working on it (as time permits), but would still appreciate your assistance.

Also, thanks for the reference to peekpoke.zip! Good find.
Anonymous

Post by Anonymous »

You are welcome!

Ted
Post Reply