keshitha
keshitha

Reputation: 5

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. I don't know in which case my java class is throwing exception. So I need to go to catch block explicitly without throwing. Can it be possible.

Please help me out.

Upvotes: 0

Views: 3065

Answers (3)

bananas
bananas

Reputation: 1196

but if the exception is thrown you will be directed towards finally block or you can post sample code so one might help you

Upvotes: -1

Andy Turner
Andy Turner

Reputation: 140524

Code in a catch block is not executed if no matching exception is thrown in the try block.

The only way to execute it is to cause the IOException to be thrown.

You can just put an explicit throw new IOException(); as the last line in the try block.

Alternatively, you might be able to pull the contents of the catch block into a separate method, which you can then invoke explicitly.

Upvotes: 2

Bhargav Kumar R
Bhargav Kumar R

Reputation: 2200

Control wont goto catch block if exception is not thrown. Put the code in finally block which you want to execute irrespective of whether exception thrown or not.

Sample:

try {

} catch() {

} finally {
//Put code here
}

Upvotes: 2

Related Questions