Can't get error code

General FreeBASIC programming questions.
azbluevw
Posts: 13
Joined: Dec 31, 2011 21:54

Can't get error code

Postby azbluevw » Nov 25, 2016 23:54

I'm compiling with -exx (which should catch array overflows) and I've tried both -lang fb and lang -qb and can't seem to get an error code with an array overflow.

Here's a short example that forces an array overflow:

Code: Select all

on error goto bad

dim a(10) as integer
dim x as integer
x = 11
a(x) = 4
end

bad:
print "error (#, line)", err, erl
sleep(50000)


This outputs:
error (#, line) 0 6


It correctly identifies line 6 as the source of the error, but says the error code is 0 (no error) when it should say 6.

I feel like I'm making some silly mistake.
MrSwiss
Posts: 3215
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Can't get error code

Postby MrSwiss » Nov 26, 2016 0:17

azbluevw wrote:By compiling with -exx (which should catch array overflows) -lang fb
Does catch the Fault, but: different to what you seem to expect.
On compilation it (-exx) will throw the Error (no code to chatch it, is needed by you).
-exx automatically adds the necessary code ...
azbluevw
Posts: 13
Joined: Dec 31, 2011 21:54

Re: Can't get error code

Postby azbluevw » Nov 26, 2016 2:30

Yes, "catch" was the wrong term; nevertheless my problem remains: the error is indeed thrown and caught, it is passed to my error handler, but the error code is incorrect.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Can't get error code

Postby MichaelW » Nov 26, 2016 5:40

Using:

Code: Select all

print err, erl

in the error handler, I get: 6 6
fxm
Posts: 9123
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can't get error code

Postby fxm » Nov 26, 2016 6:07

See documentation at /wiki/KeyPgErr, paragraph "Note" in "Description":
Note: Care should be taken when calling an internal function (such as Print) after an error occurred, because it will reset the error value with its own error status. To preserve the Err value, it is a good idea to store it in a variable as soon as the error handler is entered.

Proposed code modifying:

Code: Select all

on error goto bad

dim a(10) as integer
dim x as integer
x = 11
a(x) = 4
end

bad:
dim err0 as integer
err0 = err
print "error (#, line)", err0, erl
sleep(50000)
MrSwiss
Posts: 3215
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Can't get error code

Postby MrSwiss » Nov 26, 2016 11:00

There are Helper's available, to prevent that sort of "Array out of Bounds" Condition:

Code: Select all

Dim As LongInt a(10)

For i As ULongInt = LBound(a) to UBound(a) ' Helpers, getting current Bounds of Array
    a(i) = i * 1000 ' fill the array
    print a(i) ' output filled values
Next

Print : Print "any key press: QUIT's ! ";
Sleep
azbluevw
Posts: 13
Joined: Dec 31, 2011 21:54

Re: Can't get error code

Postby azbluevw » Dec 03, 2016 14:25

Ah, thank you so much MichealW and fxm! I knew I had to be making some silly mistake :)

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests