异常捕捉不到,请指教!
这个程序是用来返回键盘输入的各种类型的数据项的方法
这是第一个文件,
package FormattedInput;
import java.io.*;
public class FormattedInput {
private int readToken( ) {
try {
ttype = tokenizer.nextToken( );
return ttype;
}catch(IOException e ) {
e.printStackTrace( System.err );
System.exit( 1 );
}
return 0;
}
public int readInt( ) throws InvalidUserInputException {
if( readToken( ) != tokenizer.TT_NUMBER ) {
throw new InvalidUserInputException( " readInt( ) faild ."
+ "Input data not numeric." );
}
if( tokenizer.nval > ( double ) Integer.MAX_VALUE ||
tokenizer.nval < ( double ) Integer.MIN_VALUE ) {
throw new InvalidUserInputException( " readInt( ) faild ."
+ "Input data not numeric." );
}
if( tokenizer.nval != ( double ) ( int ) tokenizer.nval ) {
throw new InvalidUserInputException( " readInt( ) faild ."
+ "Input data not numeric." );
}
return ( int ) tokenizer.nval;
}
public double readDouble( ) throws InvalidUserInputException {
if( readToken( ) != tokenizer.TT_NUMBER ) {
throw new InvalidUserInputException( " readDouble( ) faild ."
+ "Input data not numeric." );
}
return tokenizer.nval;
}
public float readFloat( ) throws InvalidUserInputException {
if( readToken( ) != tokenizer.TT_NUMBER ) {
throw new InvalidUserInputException( " readFloat( ) faild ."
+ "Input data not numeric." );
}
return ( float ) tokenizer.nval;
}
public String readString( ) throws InvalidUserInputException {
if( readToken( ) == tokenizer.TT_WORD || ttype == '\'' || ttype == '\"' ){
return tokenizer.sval;
}else {
throw new InvalidUserInputException( " readString( ) faild ."
+ "Input data is not a string." );
}
}
private StreamTokenizer tokenizer = new StreamTokenizer(
new BufferedReader(
new InputStreamReader( System.in) ) );
private int ttype;
public class InvalidUserInputException extends Exception {
public InvalidUserInputException( ) { };
public InvalidUserInputException( String message ) {
super( message );
}
}
}
这是第二个文件 用来执行
import FormattedInput.*;
import java.io.*;
public class TestFormattedInput {
public static void main( String [ ] args ) {
FormattedInput kb = new FormattedInput( );
for( int i = 0; i < 5; i++ ) {
try {
System.out.println("Enter an integer: " );
System.out.println("Intger read: " + kb.readInt( ) );
System.out.println("Enter a double value: " );
System.out.println("Double value read: " + kb.readDouble( ) );
System.out.println("Enter a float value: " );
System.out.println("Double float read: " + kb.readFloat( ) );
System.out.println("Enter a string: " );
System.out.println("String read: " + kb.readString( ) );
}catch( InvalidUserInputException e) {
System.out.println("InvalidUserInpuException thrown.\n"
+ e.getMessage( ) );
}
}
}
}
编译的时候提示是这样:
symbol : class InvalidUserInputException
location: class TestFormattedInput
}catch( InvalidUserInputException e) {
^
估计是我在第一个文件上自己定义的异常,在这里捕捉不到啊 还是?