简体   繁体   English

澄清如何在Java中引发和捕获自定义异常

[英]Clarification on how throwing and catching custom exceptions in Java

So I have this program that is basically recreating SQL in java. 所以我有这个程序,它基本上是用Java重新创建SQL。 I have this "driver" class that sorta controls the whole thing. 我有一个“驱动程序”类,可以控制整个事情。 It calls and instantiates various classes and methods to make it all work. 它调用并实例化各种类和方法以使其全部起作用。 With in this I have this method 在这个我有这种方法

public void checkCommand(String input) {

for (Command c : commands) {
    if (c.matches(input)) {
        try {
            c.execute();
        } catch (MyException e) {
            System.out.println(e.getMessage());
        }
        return;
    }
}
System.out.println("That was not a valid command.");

Execute is a method that will get called on every command entered. Execute是一种在输入的每个命令上都会调用的方法。 So every thing goes through this at some point. 因此,每件事都会在某个时候经历。 Within the DefineTableCommand class, execute contains 在DefineTableCommand类中,执行包含

database.addTable(tableName, fieldList);

database is a singleton instance of the Object I'm using to to store all the tables. 数据库是我用来存储所有表的对象的单例实例。 The addTable command calls a few other things and so on. addTable命令调用其他一些东西,依此类推。 Within these, I have some methods that could go wrong, like the table name already exists or something along those line. 在这些方法中,我有一些可能出错的方法,例如表名已经存在或类似的东西。

public void addTable(String tableName, String fieldList) throws MyException {
    Table table;
    table = new Table(tableName, fieldList);
    if (instance.checkForDuplicates(tableName)) {
       throw new MyException(
            "There was a table with that name already present.");
    } else {
         instance.tableCollection.put(tableName, table);
    }
}

Now, What I want to happen, and what I thought would happen, was that the exception would be thrown from addTable, the exception would be passed up the stack to the catch in the checkCommand. 现在,我想发生的事情以及我想发生的事情是,将从addTable抛出异常,该异常将在堆栈中传递给checkCommand中的catch。 This does not seem to be the case and I'm not sure I understand why. 似乎并非如此,我不确定我理解为什么。

To explain further. 进一步解释。 The user is given a prompt in which they enter various commands (define table emp having fields (name varchar) for example) if there is an issue with their command, they should see an error message, but the program should keep running. 提示用户输入各种命令(例如,定义具有字段(名称为varchar的字段)的表emp)的命令,如果他们的命令有问题,他们应该看到错误消息,但程序应继续运行。 so lets say they enter a table with the same name as a table already present, they should be given an error message and the program continues with no new table. 因此,可以说他们输入了一个与已经存在的表同名的表,应该给他们一个错误消息,并且程序将继续运行而没有新表。 Instead no error message is printed, but the table still isn't added. 而是不会打印任何错误消息,但仍未添加该表。

Make sure that part of the signature of execute() is: 确保execute()的签名部分为:

    execute() throws MyException

And in its implementation doesn't catch MyException like: 并且在其实现中不会捕获MyException,例如:

    //eg
    void execute() throws MyException
    {
      database.addTable(tableName, fieldList);
     //any code
    }

If this is the case. 如果是这样的话。

确保在Command类execute()方法引发MyException之前没有捕获它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM