Мне нужна помощь, чтобы выбросить свое исключение. Я хочу проверить, является ли созданный мной фиктивный файл недействительным, скинуть мой BadFileException
и вывести сообщение, но оно не работает. Я не уверен, что не так, поскольку исключение не выдается, я не совсем уверен, какое условие я могу использовать, чтобы вызвать исключение, основываясь на инструкциях в моем TODO
//TODO5: Add all the remaining code to open a file
// and write the account details in a file
// The format of the account to save should be a line like this:
// FirstName,LastName,AccountNumber,StartDate
// John,Winston,156-5555555,Wed Jan 23 00:00:00 EST 2019
// You must catch the IOException and throw a
// BadFileException with message : Error Writing To File
// The out must be caught by the calling method in
// AccountLauncher class
public class b {
static String fileName = "/myFolder/data.txt";
static Account a = new Account();
public static boolean dff() {
//File f = new File("filename.txt");
try {
File f = new File("/myFolder/data.txt");
FileWriter w = new FileWriter(fileName);
w.write(a.toString());
w.close();
if(f.exists()==false) {
throw new BadFileException("Error Writing To File");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public static void main(String[] args) {
// Save the account details in a file
b b1 = new b();
try {
if ( b1.dff() ) {
System.out.println("Account Details Saved.");
}
} catch (BadFileException e) {
System.out.println( e.getMessage());
}