handle FileNotFoundException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minifish
    New Member
    • Oct 2008
    • 9

    handle FileNotFoundException

    I'm calling a method that need to open up the af.sh file.
    Because the file does not exist, I get error:

    java.io.FileNot FoundException: \file\af.sh (The system cannot find the path specified)

    I can't change this method but I want to run all other parts of the method.
    Is there anything I can do to catch this exception while continue running the method?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Yes. You wrap the code around a try/catch block and catch that FileNotFoundExc eption exception. You can read a tutorial about exceptions for further details.
    Hint: There is one in the howTos area.

    Comment

    • karthickkuchanur
      New Member
      • Dec 2007
      • 156

      #3
      post the code to give the detail

      Comment

      • minifish
        New Member
        • Oct 2008
        • 9

        #4
        I get
        Unreachable catch block for FileNotFoundExc eption. This exception is never thrown from the try statement body

        when I do

        try {
        _help= new HelpMethod();
        }catch(FileNotF oundException e){

        }

        It's try to create a log when I'm making HelpMethod
        Any suggestion will be great,

        Thank you

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by minifish
          I get
          Unreachable catch block for FileNotFoundExc eption. This exception is never thrown from the try statement body
          Your compiler doesn't lie: nowehere in the code in the try block can such an
          exception be thrown; the compiler figured it out and it whines about it.

          kind regards,

          Jos

          Comment

          • itsraghz
            New Member
            • Mar 2007
            • 124

            #6
            Originally posted by minifish
            Code:
                      try {
            			_help= new HelpMethod();
            		}catch(FileNotFoundException e){
            			
            		}
            The method being called HelpMethod() inside the try block does NOT throw an exception. Then how (and why should) can it catch such FileNotFoundExc eption??

            That's the reason the compiler complains "an unreachable catch block".

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by itsraghz
              The method being called HelpMethod() inside the try block does NOT throw an exception. Then how (and why should) can it catch such FileNotFoundExc eption??
              Well you complained about the exception being thrown at you; but it most
              certainly didn't come from that part of your code. You have to be a bit more
              detailed: somewhere in your code a file is not found (hence the exception)
              so you should find a place where you can catch it and continue with your
              code as you described in your OP.

              kind regards,

              Jos

              Comment

              Working...