Reputation: 21
I am trying to learn to work with error handling/throwing exceptions in Java. I have a class, UserDatabase, holding a collection of students, and it should also save the collection to a file.
What I'm fairly sure that I haven't done correctly, is the methods for file handling. Method public boolean saveDatabase should not throw exceptions and be handled in a try-catch, and use the encode method from the student class on Student objects, to write every object as a line in the file. This one is compiling, but it looks off to me. In the book it says write the method public boolean saveDatabase(); and as you can see I changed the header for it to make sense to me. This, mainly because I don't know how to write a method with the header ending with ();
Method public boolean loadDatabase should also handle possible IO errors and return false if one occurs. For every line in the field it should create a student object by the constructor public Student(String encodedStudent) from the sudent class. If a line in the file cannot be decoded as a student it should throw a new exception, DatabaseFormatException (this is a seperate class). This one is also listed as public boolean loadDatabase(); in the book. Let's face it, this one is completely off. I have no idea what to do and I have been working with it for hours and hours, reading books, reading online, I am lost.
Here's my code:
/**
* 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");
}
}
Upvotes: 0
Views: 317
Reputation: 57114
Your code should be
public boolean saveDatabase() {
try {
// maybe do some more work...
userdatabase.saveDatabase();
return true;
} catch (IOException e) {
return false;
}
}
Simply return true/false
depending on wether an exception occurred or not. Drop the saved
since you no longer need it. And drop the encode
since you did not need it in the first place and never assigned a value to it.
Upvotes: 0