'How to throw my own Exception with a pre written method in Java?
I have written my own Exception but I can't throw it.
The application still terminates and gives me the standard error Message:
Exception in thread "main" java.nio.file.NoSuchFileException: H:\db_sort\pdfs\test_5_database
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
at java.nio.file.Files.move(Unknown Source)
at ExceptionTest.filemove(ExceptionTest.java:22)
at ExceptionTest.main(ExceptionTest.java:9)
The application has to move some files but it could be that the file that should be moved isn't existing. Then the program shall just print out that the file couldn't be moved and then the program shall move on to the next file.
Here is a piece of my code in which I want to throw the Exception. The file test_5_database isn't existing. Then it shall just print out:
The File has been renamed, moved or deleted and those changes are not synchronized with the database.
Here is my code (I am not posting my whole code, I will just post my Exception problem):
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ExceptionTest {
public static void main(String[] args) throws IOException {
try {
filemove();
System.out.println("The file has been moved!");
}
catch(NoSuchFileException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
private static void filemove() throws IOException, NoSuchFileException{
Files.move(Paths.get("H:/db_sort/pdfs/test_5_database"),Paths.get("H:/db_sort/pdf s/2019-08/test_5_database"));
}
}
class NoSuchFileException extends Exception{
NoSuchFileException(){
super("The file has been moved, renamed or deleted and those changes
are not synchronized with the database");
}
}
I hope that someone can help me and can explain me how I can throw my own Error Message. After displaying the error message the application shall continue with moving files.
Sorry if I made some stupid mistakes in my code, I am quite new to the topic. English is not my mother tongue so please excuse any grammatical or spelling mistakes. I hope you have a nice day.
Solution 1:[1]
The method Files.move(...)
throws a NoSuchFileException
which comes from the package java.nio.file
.
Your implementation of NoSuchFileException
may have the same name, but it is never thrown. So you have to catch
the java.nio.file.NoSuchFileException
and throw your own my.package.NoSuchFileException
.
But maybe it is best (to avoid all confusion) to rename your Exception in for example FileNotSynchronizedException
. You could do it like this:
First option (you may want to remove the imports for NoSuchFileException)
private static void filemove() throws IOException, NoSuchFileException{
try{
Files.move( Paths.get("H:/db_sort/pdfs/test_5_database"),Paths.get("H:/db_sort/pdf s/2019-08/test_5_database"));
}catch(java.nio.file.NoSuchFileException e){
throw new my.package.NoSuchFileException();
}
}
Second Option
private static void filemove() throws IOException, FileNotSynchronizedException{
try{
Files.move( Paths.get("H:/db_sort/pdfs/test_5_database"),Paths.get("H:/db_sort/pdf s/2019-08/test_5_database"));
}catch(NoSuchFileException e){
throw new my.package.FileNotSynchronizedException();
}
}
See the the different throws
clause in the method header? You simply changed the Exception by throwing your own.
IMHO the second option is better, you would avoid conflicts with the existing Exceptions from java.nio.files
and it gives you the opportunity to make more specific exceptions. For example if you want to catch the IOException as well and throw your own, then just add another catch clause.
Solution 2:[2]
If you create new exception give it name different from the existing one.
To thorow exception you should write:
throw new ExceptionName("message");
for example:
throw new UnsupportedOperationException();
If you want to throw exception when file does not exist:
if(checkIfFileExists) {
throw new ExceptionName("message");
}
before you perform operation.
In your case the best approach is to change catch (creating new exception is unnecessary in that case), for example:
catch(NoSuchFileException e) {
System.out.println("The file has been moved, renamed or deleted and those
changes are not synchronized with the database");
System.out.println(e.getMessage());
e.printStackTrace();
}
Solution 3:[3]
You can throw a custom exception like below, hope my understanding is correct.
package test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ExceptionTest {
public static void main(String[] args) throws NoSuchFileException {
try {
filemove();
System.out.println("The file has been moved!");
}
catch(IOException e) {
throw new NoSuchFileException();
}
}
private static void filemove() throws IOException{
Files.move(Paths.get("H:/db_sort/pdfs/test_5_database"),Paths.get("H:/db_sort/pdf s/2019-08/test_5_database"));
}
}
class NoSuchFileException extends Exception{
NoSuchFileException(){
super("The file has been moved, renamed or deleted and those changes are not synchronized with the database");
}
}
Output :
Exception in thread "main" test.NoSuchFileException: The file has been moved, renamed or deleted and those changes are not synchronized with the database
at test.ExceptionTest.main(ExceptionTest.java:15)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | GameDroids |
Solution 2 | L.dev |
Solution 3 | Rama 1220 |