Why is there no catch?

32 views Asked by At

Im trying to delete TXT-files, but I always get the same error message. It's nearly the same code as the one I found on the Internet.

                for (int i = 0; i < datei.length; i++)
                {
                        try
                        {
                            loeschenDatei = datei[i].delete();

                            if (loeschenDatei)
                            {
                                System.out.println(datei[i] + " wurde geloescht!");
                            }
                            else 
                            {
                                System.out.println(datei[i] + " konnte nicht geloescht werden!");
                            }
                        }
                        catch (IOException ex)
                        {
                            ex.printStackTrace();
                        }

                }

I always get the error:

unreachable catch block for ioexception. this exception is never thrown from the try statement body.

2

There are 2 answers

4
arjabbar On BEST ANSWER

This means that nothing within your try block can throw an Exception of type IOException. The only thing that I'm unsure of is datei[i].delete(). Check out that method signature in your IDE and see if at the end it throws IOException or something like that. If that method doesn't throw anything, then remove your try catch block altogether.

0
Rajesh On

unreachable catch block for ioexception - compiler code validation is suggesting that. Possibly IO exceptions already handled in delete method.

In case you are unsure which exceptions may be thrown by your code, you could change IOException to Exception.