简体   繁体   English

如何在不引发异常的情况下捕获块

[英]How to go to catch block without throwing exception

I have a java class which is throwing IOException .I have some code in Catch block which i need to debug. 我有一个抛出IOException的java类。我在Catch块中有一些代码需要调试。 I don't know in which case my java class is throwing exception. 我不知道在哪种情况下我的java类会抛出异常。 So I need to go to catch block explicitly without throwing. 因此,我需要明确地捕获块而不抛出。 Can it be possible. 有可能吗

Please help me out. 请帮帮我。

Control wont goto catch block if exception is not thrown. 如果未引发异常,则控件不会转到catch块。 Put the code in finally block which you want to execute irrespective of whether exception thrown or not. 无论是否引发异常,都将代码放入要执行的finally块中。

Sample: 样品:

try {

} catch() {

} finally {
//Put code here
}

Code in a catch block is not executed if no matching exception is thrown in the try block. 如果try块中没有抛出匹配异常,则catch块中的代码将不会执行。

The only way to execute it is to cause the IOException to be thrown. 执行它的唯一方法是引发IOException

You can just put an explicit throw new IOException(); 您可以显式throw new IOException(); as the last line in the try block. 作为try块的最后一行。

Alternatively, you might be able to pull the contents of the catch block into a separate method, which you can then invoke explicitly. 或者,您可以将catch块的内容拉入一个单独的方法中,然后可以显式调用该方法。

但是如果引发了异常,您将被定向到finally块,或者可以发布示例代码,这样可能会对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM