0

This might seem like a weird question, but is it possible to "catch"(know) if there is a filenotfoundexception in the stack trace? I am asking this because the class I am implementing (not mine) does not throw the exception, it catches it and prints the stack trace.

So, in other words, can I display a JOptionPane with a custom message when a filenotfoundexception is in the stack trace?

Thanks!

problemo
  • 629
  • 4
  • 13
  • You may be able to check if the file exists beforehand. – Bernhard Barker May 10 '13 at 15:07
  • 1
    yes, I could, but why run code twice? – problemo May 10 '13 at 15:11
  • You'd be putting in a single reasonably fast check before running the code. Not guaranteed to work all the time, since the file can be removed in that millisecond (or hopefully around that amount of time) between your check and running the code (you may be able to lock it some way or another as a work-around), but probably a lot simpler than some voodoo magic to redirect the standard error output (if even possible) and doing a string-search for something like "FileNotFoundException". – Bernhard Barker May 10 '13 at 15:22
  • On a side note, tell whomever wrote that class to please rewrite it properly (unless it wasn't intended to be used as a library-like class). – Bernhard Barker May 10 '13 at 15:23
  • :), It's some guy online that wrote it but anyway, since there is no solution, I am checking for the file with the myFile.isFile() && myFile.canRead(). Thanks for the answer! – problemo May 10 '13 at 15:34

2 Answers2

1

Here's a way using System.setErr and piped streams:
(it's entirely possible that there's a better way or it can be simplified)

public static void badFunctionCall()
{
  new FileNotFoundException("The file could not be found!").printStackTrace();
}

public static void main(String[] args) throws IOException
{
  PipedOutputStream writer = new PipedOutputStream();
  PipedInputStream reader = new PipedInputStream(writer);
  PrintStream p = new PrintStream(writer);
  System.setErr(p);

  badFunctionCall();

  p.close(); // do this *before* reading the input stream to prevent deadlock
  int c;
  StringBuilder builder = new StringBuilder();
  while ((c = reader.read()) != -1)
     builder.append((char)c);
  if (builder.toString().contains("java.io.FileNotFoundException: "))
     System.out.println("An error occurred! Caught outside function.");
  reader.close();
}

Test.

Note that it's probably not advisable to connect streams in the same thread, or at least one has to be really careful, as one can very easily run into deadlock.

But a much simpler:

file.isFile() && file.canRead()

before the function call, while not 100% reliable (a work-around by locking the file for the duration of the call may be possible), is preferred.

Community
  • 1
  • 1
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

I try it:

[not your class which prints the stack trace:]

    catch ( Exception e ) {
      String trace = e.toString(); 
        // there are better methods than toString() in newer JDK versions, 
        // but for now it should work
      if ( trace.toLowerCase().indexOf( "filenotfoundexception" ) >= 0 ) {
        // there is one
        JOptionPane....what ever you want...
      }
    }

Update:

you will not get the suppressed exceptions http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getSuppressed() but I strongly assume your FileNot...Exception is not one of those...

xtraclass
  • 445
  • 3
  • 7
  • Where would i put the catch? Like I said, there is no try/catch block in my class because the other class is catching it not throwing it. – problemo May 10 '13 at 15:13