开发者

Commenting code in try-catch, Java

开发者 https://www.devze.com 2023-03-17 14:17 出处:网络
I have such piece of code in my project: try { downloadFile(); unzipFile(); } catch (IOException e) { System.out.println(\"Can\'t download file!\");

I have such piece of code in my project:

try {
    downloadFile();
    unzipFile();
} catch (IOException e) {
    System.out.println("Can't download file!");
    e.printStackTrace();
}

Method downloadFile() trhows exception IOException, and method unzipFile() doesn't throw any exceptions at all.

Now I'm trying to debug this code, my method downloadFile() works fine, so I want to comment it and see how method unzipFile() works. Let's assume also that file size is 100 Mb :)

When I'm commenting line with 'downloadFile()' I get this error:

unreachable block for IOException. This exception is never 开发者_StackOverflow中文版thrown from the try statement body

In order to debug my code, I'm inserting line like

if (true) return;

in method downloadFile(), and then deleting it.

I'm sure that I'm doing it wrong way.

Could you please give me an example of how java masters are doing in such situations. Thanks.


As Rob mentioned in the comments, tests test specific things/methods/behaviors. If you want to test unzipFile then write a reproducable test case for unzipFile and don't mess with anything else (ie downloadFile) that could have its own problems. Then, you test unzipFile with known input and output data.

In other words, if you want to test unzipFile test that, and not everything else with it.

Other than that a simple solution for you right now would be this, I think:

try {
    downloadFile();
} catch (IOException e) {
    System.out.println("Can't download file!");
    e.printStackTrace();
}
unzipFile();

so you can easily comment out the whole try/catch block


If you comment out downloadFile();, you should also comment out the try-catch block, because it doesn't catch anything anymore.


Why to use try/catch if you are sure that method (unzipFile()) do not throw any exception.


The error itself says all "unreachable block for IOException. This exception is never thrown from the try statement body".

As you said your unzipFile() method do not throw any exception so why to wrap it with try..catch? Just remove or comment try..catch() to run/debug it. That is just write unzipFile() in code nothing else. If unzipFile() depends on downloadFile() then it might result in error.


You shouldn't be commenting out downloadFile(). The message is telling you the truth: no code in the try block throws that exception.

I usually use an IDE with a debugger for this sort of thing. You can use Eclipse or NetBeans; I prefer IntelliJ from Jet Brains.


Usually you do not need to comment anything if you have the appropriate tools. E.g. u did a mistake in unzipFile your IDE preferable eclipse will show you a stacktrace. If you click on the position in the stack trace you will automatically get the file were the error occured marking the line.

If you can reproduce this error you can set a breakpoint right before this line to inspect variables and class attributes. If you identify the block which causes the problem u can set a breakpoint right before this block and so on.

0

精彩评论

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