Can't get error code

General FreeBASIC programming questions.
Post Reply
azbluevw
Posts: 16
Joined: Dec 31, 2011 21:54

Can't get error code

Post by azbluevw »

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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Can't get error code

Post by MrSwiss »

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: 16
Joined: Dec 31, 2011 21:54

Re: Can't get error code

Post by azbluevw »

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

Post by MichaelW »

Using:

Code: Select all

print err, erl
in the error handler, I get: 6 6
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can't get error code

Post by fxm »

See documentation at 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: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Can't get error code

Post by MrSwiss »

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: 16
Joined: Dec 31, 2011 21:54

Re: Can't get error code

Post by azbluevw »

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