开发者

android exception for validating file exist not working

I've been working with Eclipse ADT for about 2 months. In that time, I have a small utility that allows me to select an IP Address and Port, and then send a file to that combo. The utility works as intended, but when I type in the wrong file name, the application hangs.

@Override
   public void run() {
      if (data != null) {
         this.send(data);
      } else if (this.file != null) {
         if (file.exists()) {
            this.send(file);
         } else {
            transferError = new FileNotFoundException("The specified file could not be found");
         }
      }
   }


I've even tried to do the following in hopes that one or the other would throw, but I am unsuccessful in both.

public void run() {
      if (data != null) {
         this.send(data);
      } else if (this.file != null) {
         if (file.exists()) {
            this.send(file);
         } else {
            transferError = new FileNotFoundException("The specified file could not be found");
         }
      }try {
         throw new Exception("blah blah bla开发者_运维知识库h");
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

I've jockeyed around the exception, I've added the one above, I've tried placing it in different places, and all unsuccessful. Again, I'm exceptionally new to this, and got here from basically mincing various tcp client codes. Aside of creating a way to throw the exception correctly, please help me understand why the first one isn't working and why the one you suggest is.


in your else block you aren't throwin the transferError you create.

throw transferError;

However you probably won't be able to do that because FileNotFoundException is a checked exception and the run() method doesn't declare any thrown exceptions. You probably need to find a different way to present the error to the user, like with a Toast or something.

Your second block doesn't work because you are catching the exception you throw.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜