27

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.

firas
  • 1,463
  • 4
  • 19
  • 42

7 Answers7

22

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.

luk2302
  • 55,258
  • 23
  • 97
  • 137
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • ! This idea can be extended to throw any type of exception. :) – Jayan Mar 05 '11 at 17:45
  • 3
    Using Eclipse you can also add a break point somewhere and a watch expression "throw new FooBarException". Only problem is, it will be evaluated every time the VM is suspended. – Axel Knauf Mar 05 '11 at 19:17
  • 3
    @Axel you mark the expression as disabled, and then anytime you want it to be thrown you just right click on it and 'Reevaluate Watch Expression' and it gets thrown a single time. – Patrick Mar 17 '11 at 22:42
19

In eclipse Debug perspective, in Expressions view, just add new expression: throw new Exception("test"). I suppose NetBeans has something similar.

user443854
  • 7,096
  • 13
  • 48
  • 63
2

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

Tim Post
  • 33,371
  • 15
  • 110
  • 174
mericano1
  • 2,914
  • 1
  • 18
  • 25
0

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.

mdm
  • 12,480
  • 5
  • 34
  • 53
  • When the internet is disconnected, or the ntwork card is down, the thread blocks on the second line, JUST LIKE I SAID in the question :) – firas Mar 05 '11 at 17:25
  • 1
    It may block while the request times out, so just wait. There is also nothing to stop you from throwing your own IOException. – mdm Mar 05 '11 at 17:26
  • In other words, I need to inject - Invoke an exception while running, without affecting current code ! – firas Mar 05 '11 at 17:30
0

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

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.

Community
  • 1
  • 1
typo.pl
  • 8,812
  • 2
  • 28
  • 29
0

You can also throw Exceptions with coditional breakpoints. With this way is not necessary modify your code.

wil
  • 11
  • 2