How Do I Throw an Exception?

K

kvnsmnsn

I've written a little program that takes two strings as input. It
converts the first string to an integer index and then applies that
index to the second string, using the <charAt> method of class
<String>. I wanted to check for the possibility that the index might
be too long for the second string, so in such a case my code tries to
"throw" exception <ArrayIndexTooLargeException>, which I declare ear-
lier in my program. However, when I try to compile this program I get
the error message:

[kvnsmnsn@phuture Lab3]$ javac Aioob.java
Aioob.java:15: cannot find symbol
symbol : variable ArrayIndexTooLargeException
location: class Aioob
{ throw ArrayIndexTooLargeException;
^
1 error
[kvnsmnsn@phuture Lab3]$

Anybody have any idea what I'm doing wrong, and what I'd need to do to
accomplish what I'm trying to accomplish? My code follows.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

public class Aioob
{
private class ArrayIndexTooLargeException
extends ArrayIndexOutOfBoundsException
{}

public static void main ( String[] arguments)
{
int index;

if (arguments.length == 2)
{ try
{ index = Integer.parseInt( arguments[ 0]);
if (index >= arguments[ 1].length())
{ throw ArrayIndexTooLargeException;
}
System.out.println
( "\"" + arguments[ 1] + "\".charAt( " + index + ") == '"
+ arguments[ 1].charAt( index) + "'.");
}
catch (NumberFormatException excptn)
{ System.out.println
( "Couldn't convert \"" + arguments[ 0] + "\" to an
integer.");
}
catch (ArrayIndexTooLargeException excptn)
{ System.out.println
( "Index " + arguments[ 0] + " out of bounds for string \""
+ arguments[ 1] + "\".");
}
}
else
{ System.out.println( "Usage is\n Aioob <index> <string>");
}
}
}
 
P

Paul Tomblin

In a previous article, (e-mail address removed) said:
symbol : variable ArrayIndexTooLargeException
location: class Aioob
{ throw ArrayIndexTooLargeException;
^

ArrayIndexTooLargeException is an object. You have to create it.

throw new ArrayIndexTooLargeException();
 
M

Mark

I've written a little program that takes two strings as input. It
converts the first string to an integer index and then applies that
index to the second string, using the <charAt> method of class
<String>. I wanted to check for the possibility that the index might
be too long for the second string, so in such a case my code tries to
"throw" exception <ArrayIndexTooLargeException>, which I declare ear-
lier in my program. However, when I try to compile this program I get
the error message:

[kvnsmnsn@phuture Lab3]$ javac Aioob.java
Aioob.java:15: cannot find symbol
symbol : variable ArrayIndexTooLargeException
location: class Aioob
{ throw ArrayIndexTooLargeException;
^
1 error
[kvnsmnsn@phuture Lab3]$

Anybody have any idea what I'm doing wrong, and what I'd need to do to
accomplish what I'm trying to accomplish? My code follows.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

####################################################################

public class Aioob
{
private class ArrayIndexTooLargeException
extends ArrayIndexOutOfBoundsException
{}

public static void main ( String[] arguments)
{
int index;

if (arguments.length == 2)
{ try
{ index = Integer.parseInt( arguments[ 0]);
if (index >= arguments[ 1].length())
{ throw ArrayIndexTooLargeException;
}
System.out.println
( "\"" + arguments[ 1] + "\".charAt( " + index + ") == '"
+ arguments[ 1].charAt( index) + "'.");
}
catch (NumberFormatException excptn)
{ System.out.println
( "Couldn't convert \"" + arguments[ 0] + "\" to an
integer.");
}
catch (ArrayIndexTooLargeException excptn)
{ System.out.println
( "Index " + arguments[ 0] + " out of bounds for string \""
+ arguments[ 1] + "\".");
}
}
else
{ System.out.println( "Usage is\n Aioob <index> <string>");
}
}
}

Replace

throw ArrayIndexTooLargeException

with

throw new ArrayIndexTooLargeException()

You will still get an error, but you will be farther along in reaching
your goal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top