java - 定义我自己的异常时找不到符号

标签 java exception error-handling

这个问题在这里已经有了答案:





What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?

(17 个回答)


2年前关闭。




有人可以告诉我为什么我在编译时得到“找不到符号”。

这就是我定义自己的异常的方式:

public void setIdNumber(int i)throws InvalidEmployeeId
  {
  idNumber = i;

   if ((idNumber <= 0))
     {
        throw new InvalidEmployeeId("invalid ID. Please input numeric ID");
     }

  }

找不到标志:
public void setIdNumber(int i)throws InvalidEmployeeIdsymbol: class InvalidEmployeeId
下面是演示程序中的 try and catch 语句:
 try 
   {
     id = Integer.parseInt(input);
     worker.setIdNumber(id);
   } catch (invalidEmployeeId e)
   {
     System.out.println(e.getMessage());
   }

提前致谢;

最佳答案

将此类添加到您的代码中:

public class InvalidEmployeeId extends Exception
{

public InvalidEmployeeId(String string)
{
    super(string);
}

}

只是说新并没有定义Exception为你。此外,您必须始终如一地使用类名。
try 
 {
 id = Integer.parseInt(input);
 worker.setIdNumber(id);
 } catch (InvalidEmployeeId e) //must be a capital I!
 {
 System.out.println(e.getMessage());
 }

如果你 已定义 一个 ivalidEmployeeId Exception子类,那么你必须改变这一行:
 throw new invalidEmployeeId("invalid ID. Please input numeric ID"); 

关于java - 定义我自己的异常时找不到符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894809/

相关文章:

java - 抛出以指示失败 "sanity check"的最佳内置 JRE 异常是什么?

java - 在添加到 Set 之前检查 Collection 是否为 null 的不同方法的优缺点是什么?

angular - Angular 中的通用 http 错误处理

php - 使用 DB::query 处理 Silverstripe 中的 MySQL 错误

java - 无法使用 Hibernate 空间将几何对象持久保存到 Oracle

java - 如何为每个打印的 "line"创建列表或数组列表

java - JSP页面在Servlet程序中不起作用

java - 创建自定义 Lint Java 检测器

exception - FirebaseRemoteConfigFetchThrottledException ? #AskFirebase

angular - 单实例 Angular 2+ 通知服务/错误处理程序