-2

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.

MrTux
  • 32,350
  • 30
  • 109
  • 146
Moritz H.
  • 3
  • 1
  • The message suggests the delete you're using doesn't throw IOException.What is the type of `datei`? Without knowing that, and knowing what specific `delete()` method you're invoking, it is hard to say exactly what is going on. – knolleary Jun 08 '15 at 21:01

2 Answers2

1

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.

arjabbar
  • 6,044
  • 4
  • 30
  • 46
  • Thank you, so should I just delete the whole try&catch? – Moritz H. Jun 08 '15 at 21:09
  • I'm assuming so. Like I said though, if `delete()` throws any Exception at all then you will get a compile time error until you handle whatever exception it throws. Try removing the try catch block first and compiling the application to see if that `delete()` method throws anything. – arjabbar Jun 08 '15 at 21:16
  • @arjabbar That's incorrect....As datei[i] could be null and delete() invocation on null would throw NullPointerException. – Rajesh Jun 08 '15 at 21:19
  • Sorry, Rajesh is correct. NullPointerExceptions could be thrown but those don't have to be explicitly handled. – arjabbar Jun 09 '15 at 01:47
0

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.

Rajesh
  • 2,135
  • 1
  • 12
  • 14