在异常情况下苦苦挣扎,在Java

时间:2017-12-03 13:04:24

标签: java exception error-handling singleton try-catch

我正在尝试学习使用Java处理错误处理/抛出异常。 我有一个类,UserDatabase,持有学生集合,它还应该将集合保存到文件中。

我相当确定我还没有正确完成,是文件处理的方法。方法public boolean saveDatabase不应抛出异常并在try-catch中处理,并使用Student对象上student类的encode方法将每个对象写为文件中的一行。这个是编译,但它看起来像我。在书中它说写方法public boolean saveDatabase();正如你所看到的,我改变了它的标题对我有意义。这主要是因为我不知道如何编写一个以();

结尾的标题的方法

方法public boolean loadDatabase也应该处理可能的IO错误,如果发生错误则返回false。对于字段中的每一行,它应该由来自sudent类的构造函数public Student(String encodedStudent)创建一个student对象。如果文件中的一行无法解码为学生,则应抛出一个新的异常,即DatabaseFormatException(这是一个单独的类)。这个也被列为public boolean loadDatabase();在书里。让我们面对它,这个完全是关闭的。我不知道该做什么,我一直在用它工作几个小时,读书,在线阅读,我迷失了。

这是我的代码:

   /**
* This method should not throw exceptions.
* By using a try/catch block, we anticipate possible failures.
* We recognize that these actions might fail and throw errors.
*/
   public boolean saveDatabase() throws IOException {
   //This method is using the encode method on student objects and should
   //write each object as a line in the file. 
   String encode = null;
   boolean saved;
   try {

       encode = null;
   userdatabase.saveDatabase();
   saved = false;
}
   catch (IOException e) {
       System.out.println("Error");
       saved = false;
    }
    finally {
        if(encode.equals(students)) {
        //System.out.println("Students" + students)?;
        saved = true;
    }
    } 
   return saved;
}

   /**
    * Method loadDatabase should handle possible IO errors, and return false
    * if one occurs. Otherwise, it should return true and create a new 
Student object
    * by using the constructor public Student(String encodedStudent).
    * If a line cannot be decoded as a student, the method should throw a 
new
    * exception "DatabaseFormatException".
    * 
    */
     public boolean loadDatabase() throws IOException,DatabaseFormatException {
   //Attempting to use the String encodedStudent constructor from Student class
   String encodedStudent = null;
   boolean loaded;
   try {
       //Attempting to create possible IO errors returning false if they occur
       enco dedStudent = null;
       //UserDatabase.loadDatabase(encodedStudent);
       loaded = false;
    }
    catch(IOException e) {
        if (encodedStudent == null) {
            System.out.println("Error");
            loaded = false;
        }
    }
    //Trying a for each loop to throw a DatabaseFormatException

   for(Student student : students) {
        //This is supposed to throw a DatabaseFormatException if a 
        //line in the file cannot be decoded as a student!
       if(student.getName.isEmpty() && this.course.isEmpty()) {
            throw new DatabaseFormatException(
            "No student found");
        }
    }

1 个答案:

答案 0 :(得分:0)

您的代码应为

public boolean saveDatabase() {
   try {
        // maybe do some more work...
        userdatabase.saveDatabase();
        return true;
    } catch (IOException e) {
        return false;
    }
}

简单地返回true/false,具体取决于是否发生异常。删除saved,因为您不再需要它。并删除encode,因为您首先不需要它,并且从未为其分配值。