Reputation: 1503
Is it possible to force an exception to be thrown while debugging.
Let me give you an example: I am debugging some code where I download a page from the internet. When the Internet connection is lost, or network card is down, an IOException should be thrown, BUT instead the execution is blocking (on the second line)
URLConnection connection = requestURL.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
I need a way to force throwing an exception while debugging when the code is blocking so I can jump to the catch block.
I am using netbeans BTW.
While Debugging = added manually when the execution thread is paused.
EDIT: In other words, I need to inject - Invoke an exception while running, without affecting current code !
Thank you.
Upvotes: 28
Views: 32173
Reputation: 11
You can also throw Exceptions with coditional breakpoints. With this way is not necessary modify your code.
Upvotes: 0
Reputation: 28568
I would find the variable that is being waited on, and in the expression window in eclipse, call notify on that variable, then i would null
out that variable, so that the synchronized block would throw a NullPointerException
.
Upvotes: 22
Reputation: 7455
In eclipse Debug perspective, in Expressions view, just add new expression: throw new Exception("test")
. I suppose NetBeans has something similar.
Upvotes: 19
Reputation: 2913
I think what you need to do is wrap that code in a FutureTask with a time out and have the main thread waiting for either the time out or the completion of the task.
You can also use some additional system properties to only throw the exception if in test mode
Have a look at this post (Disclaimer, I wrote it)
Hope it helps
Upvotes: 2
Reputation: 8942
JDB has a kill
thread command where the developer can specify the thread id and the exception to be used to kill the thread.
I found two references that use the command line jdb debugger. See StackOverflow - Is it possible view/pause/kill a particular thread from a different java program running on the same JVM?, How do I use kill call in JDB?
I couldn't find any info on a similar method from Eclipse Java debugger.
Upvotes: 0
Reputation: 597116
If you thread is waiting (blocked), you can't do much with it. You'd have to interrupt()
the waiting thread. The JVM appears to have this option, you just have to find it in the debugger.
Upvotes: 0
Reputation: 12630
Why not just modify the code temporarily so that it generates an exception? (throw new IOException(...)
). Or, disconnect from the internet and try and run it.
Upvotes: 0