异常捕捉不到,请指教!

luojieone 2004-07-28 02:53:18
这个程序是用来返回键盘输入的各种类型的数据项的方法
这是第一个文件,
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) {
^
估计是我在第一个文件上自己定义的异常,在这里捕捉不到啊 还是?
...全文
103 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
maowu 2004-07-28
  • 打赏
  • 举报
回复
不是静态方法,是静态内部类.
public class InvalidUserInputException extends Exception {
改成:
public static class InvalidUserInputException extends Exception {

}catch( InvalidUserInputException e) {
改成:
}catch( FormattedInput.InvalidUserInputException e) {
luojieone 2004-07-28
  • 打赏
  • 举报
回复
或者写成静态的内部类,引用的时候把外部类的类名也写上。
这个我没有用到,我是将异常类又打了个包,在用就可以了
刚开始我也用了这个方法的 但是可能是在那里错了,没有成功!
哎,慢慢学习啊。我学习JAVA是打击了 承受了 在打击了在承受了
又~~~~~
谢谢你啊
恩 你说的静态方法是什么?可以看看你的代码吗?
shenlang 2004-07-28
  • 打赏
  • 举报
回复
同意楼上的
maowu 2004-07-28
  • 打赏
  • 举报
回复
你的InvalidUserInputException是FormattedInput的内部类,TestFormattedInput 不能这样引用。把InvalidUserInputException拿出来作为独立的类,或者写成静态的内部类,引用的时候把外部类的类名也写上。
lightsword 2004-07-28
  • 打赏
  • 举报
回复
用throws Exception捕获一下试试.

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧