Thread overview
Catching array out of bounds
Nov 05, 2009
jicman
Nov 05, 2009
Justin Johansson
Nov 05, 2009
grauzone
Nov 05, 2009
jicman
Nov 05, 2009
grauzone
Nov 05, 2009
jicman
November 05, 2009
Greetings!

I have this program,

import std.stdio;
void main()
{
  char[][] a = ["a","b","c"];
  try
  {
    writefln(a[3]);
  }
  catch (ArrayBoundsError)
  {
    writefln("error...");
  }
}

when I compile it, I get,

23:53:52.73>build -I..;c:\D\dmd\import array.d
array.d(10): Error: identifier 'ArrayBoundsError' is not defined
array.d(10): Error: ArrayBoundsError is used as a type
Error: can only catch class objects, not 'void'

This is right from the D1 Arrays help...

http://www.digitalmars.com/d/1.0/arrays.html

What am I doing wrong?

thanks,

josé


November 05, 2009
jicman Wrote:

> Greetings!
> 
> I have this program,
> 
> import std.stdio;
> void main()
> {
>   char[][] a = ["a","b","c"];
>   try
>   {
>     writefln(a[3]);
>   }
>   catch (ArrayBoundsError)
>   {
>     writefln("error...");
>   }
> }
> 
> when I compile it, I get,
> 
> 23:53:52.73>build -I..;c:\D\dmd\import array.d
> array.d(10): Error: identifier 'ArrayBoundsError' is not defined
> array.d(10): Error: ArrayBoundsError is used as a type
> Error: can only catch class objects, not 'void'
> 
> This is right from the D1 Arrays help...
> 
> http://www.digitalmars.com/d/1.0/arrays.html
> 
> What am I doing wrong?
> 
> thanks,
> 
> josé
> 

Hi josé,

You don't import modules as you show:
build -I..;c:\D\dmd\import array.d

You should be importing the array module into
your program using the following statement.

import std.array;

Also (and I' not 100% sure about this) that you might need to
declare a variable along with the catch statement like as follows
(at least that's what I always do regardless of whether or
not it may be omitted) ...

catch (ArrayBoundsError e) {
...
}

Good luck with your further D adventures,

Justin Johansson

November 05, 2009
Justin Johansson wrote:
> Also (and I' not 100% sure about this) that you might need to
> declare a variable along with the catch statement like as follows
> (at least that's what I always do regardless of whether or
> not it may be omitted) ...

And he should remember that out of bounds checking is enabled only in debug builds. In release builds, out of bounds accesses will simply lead to undefined behavior.
November 05, 2009
Justin Johansson Wrote:

> jicman Wrote:
> 
> > Greetings!
> > 
> > I have this program,
> > 
> > import std.stdio;
> > void main()
> > {
> >   char[][] a = ["a","b","c"];
> >   try
> >   {
> >     writefln(a[3]);
> >   }
> >   catch (ArrayBoundsError)
> >   {
> >     writefln("error...");
> >   }
> > }
> > 
> > when I compile it, I get,
> > 
> > 23:53:52.73>build -I..;c:\D\dmd\import array.d
> > array.d(10): Error: identifier 'ArrayBoundsError' is not defined
> > array.d(10): Error: ArrayBoundsError is used as a type
> > Error: can only catch class objects, not 'void'
> > 
> > This is right from the D1 Arrays help...
> > 
> > http://www.digitalmars.com/d/1.0/arrays.html
> > 
> > What am I doing wrong?
> > 
> > thanks,
> > 
> > josé
> > 
> 
> Hi josé,
Hi Justin.

> 
> You don't import modules as you show:
> build -I..;c:\D\dmd\import array.d
I am giving the path of where the modules are.  The array.d is the program that contains the above code.

> 
> You should be importing the array module into
> your program using the following statement.
> 
> import std.array;
there is no module std.array for D1.  I don't know about D2, but D1 does not have it.

> Also (and I' not 100% sure about this) that you might need to
> declare a variable along with the catch statement like as follows
> (at least that's what I always do regardless of whether or
> not it may be omitted) ...
> 
> catch (ArrayBoundsError e) {
> ...
> }
I tried that also.  :-) Did you even try the little program?  Copy and paste to your favorite editor and give it a try. :-)
> 

Anybody else can provide me what is wrong with the program above?

thanks,

josé
November 05, 2009
grauzone Wrote:

> Justin Johansson wrote:
> > Also (and I' not 100% sure about this) that you might need to
> > declare a variable along with the catch statement like as follows
> > (at least that's what I always do regardless of whether or
> > not it may be omitted) ...
> 
> And he should remember that out of bounds checking is enabled only in debug builds. In release builds, out of bounds accesses will simply lead to undefined behavior.

Ok, I will bite...  And why, then, it is in the array help as an option to catch the out of bounds?
November 05, 2009
jicman wrote:
> Justin Johansson Wrote:
> 
>> jicman Wrote:
>>
>>> Greetings!
>>>
>>> I have this program,
>>>
>>> import std.stdio;
>>> void main()
>>> {
>>>   char[][] a = ["a","b","c"];
>>>   try
>>>   {
>>>     writefln(a[3]);
>>>   }
>>>   catch (ArrayBoundsError)
>>>   {
>>>     writefln("error...");
>>>   }
>>> }
>>>
>>> when I compile it, I get,
>>>
>>> 23:53:52.73>build -I..;c:\D\dmd\import array.d
>>> array.d(10): Error: identifier 'ArrayBoundsError' is not defined
>>> array.d(10): Error: ArrayBoundsError is used as a type
>>> Error: can only catch class objects, not 'void'
>>>
>>> This is right from the D1 Arrays help...
>>>
>>> http://www.digitalmars.com/d/1.0/arrays.html
>>>
>>> What am I doing wrong?
>>>
>>> thanks,
>>>
>>> josé
>>>
>> Hi josé,
> Hi Justin.
> 
>> You don't import modules as you show:
>> build -I..;c:\D\dmd\import array.d
> I am giving the path of where the modules are.  The array.d is the program that contains the above code.
> 
>> You should be importing the array module into
>> your program using the following statement.
>>
>> import std.array;
> there is no module std.array for D1.  I don't know about D2, but D1 does not have it.
> 
>> Also (and I' not 100% sure about this) that you might need to
>> declare a variable along with the catch statement like as follows
>> (at least that's what I always do regardless of whether or
>> not it may be omitted) ...
>>
>> catch (ArrayBoundsError e) {
>> ...
>> }
> I tried that also.  :-) Did you even try the little program?  Copy and paste to your favorite editor and give it a try. :-)
> 
> Anybody else can provide me what is wrong with the program above?
> 
> thanks,
> 
> josé


Actually, there is a module called std.array in D1 as well. For some reason it's just not in the documentation on the homepage. The ArrayBoundsError class is defined in there. Try importing it, and see if that works. (I can't test it, as I'm using D2 myself.)

Alternatively, write catch(Exception e) or catch(Error e). Both of them are defined in object.d and are always available.

-Lars

-Lars
November 05, 2009
jicman wrote:
> grauzone Wrote:
> 
>> Justin Johansson wrote:
>>> Also (and I' not 100% sure about this) that you might need to
>>> declare a variable along with the catch statement like as follows
>>> (at least that's what I always do regardless of whether or
>>> not it may be omitted) ...
>> And he should remember that out of bounds checking is enabled only in debug builds. In release builds, out of bounds accesses will simply lead to undefined behavior.
> 
> Ok, I will bite...  And why, then, it is in the array help as an option to catch the out of bounds?

I don't quite get what you're saying, but here's the bit of the documentation that says that out of bounds checking is an optional debugging feature:

"A program may not rely on array bounds checking happening"
http://www.digitalmars.com/d/1.0/arrays.html

You can try it yourself. Just compile the program with -release. No out of bounds error will be thrown; instead the program may behave abnormally or crash (or the error remains silent).
November 05, 2009
Lars T. Kyllingstad wrote:
> jicman wrote:
>> Justin Johansson Wrote:
>>
>>> jicman Wrote:
>>>
>>>> Greetings!
>>>>
>>>> I have this program,
>>>>
>>>> import std.stdio;
>>>> void main()
>>>> {
>>>>   char[][] a = ["a","b","c"];
>>>>   try
>>>>   {
>>>>     writefln(a[3]);
>>>>   }
>>>>   catch (ArrayBoundsError)
>>>>   {
>>>>     writefln("error...");
>>>>   }
>>>> }
>>>>
>>>> when I compile it, I get,
>>>>
>>>> 23:53:52.73>build -I..;c:\D\dmd\import array.d
>>>> array.d(10): Error: identifier 'ArrayBoundsError' is not defined
>>>> array.d(10): Error: ArrayBoundsError is used as a type
>>>> Error: can only catch class objects, not 'void'
>>>>
>>>> This is right from the D1 Arrays help...
>>>>
>>>> http://www.digitalmars.com/d/1.0/arrays.html
>>>>
>>>> What am I doing wrong?
>>>>
>>>> thanks,
>>>>
>>>> josé
>>>>
>>> Hi josé,
>> Hi Justin.
>>
>>> You don't import modules as you show:
>>> build -I..;c:\D\dmd\import array.d
>> I am giving the path of where the modules are.  The array.d is the program that contains the above code.
>>
>>> You should be importing the array module into
>>> your program using the following statement.
>>>
>>> import std.array;
>> there is no module std.array for D1.  I don't know about D2, but D1 does not have it.
>>
>>> Also (and I' not 100% sure about this) that you might need to
>>> declare a variable along with the catch statement like as follows
>>> (at least that's what I always do regardless of whether or
>>> not it may be omitted) ...
>>>
>>> catch (ArrayBoundsError e) {
>>> ...
>>> }
>> I tried that also.  :-) Did you even try the little program?  Copy and paste to your favorite editor and give it a try. :-)
>>
>> Anybody else can provide me what is wrong with the program above?
>>
>> thanks,
>>
>> josé
> 
> 
> Actually, there is a module called std.array in D1 as well. For some reason it's just not in the documentation on the homepage. The ArrayBoundsError class is defined in there. Try importing it, and see if that works. (I can't test it, as I'm using D2 myself.)
> 
> Alternatively, write catch(Exception e) or catch(Error e). Both of them are defined in object.d and are always available.

...or don't. Like grauzone pointed out, you shouldn't rely on array bounds being checked. That's probably why std.array isn't in the docs.

-Lars