28

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.

3
  • 1
    aren't mock objects were invented just for this purpose? Commented Mar 5, 2011 at 17:29
  • 2
    Yes, If you were doing unit tests, but I'm not ... Commented Mar 5, 2011 at 17:31
  • You can write a unit test reproducing your issue and debug it. However if your system is tightly coupled this might be a difficult task. Commented Mar 5, 2011 at 17:35

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

! This idea can be extended to throw any type of exception. :)
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 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.
19

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

1 Comment

NetBeans seems to not have that.
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

Comments

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.

3 Comments

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 :)
It may block while the request times out, so just wait. There is also nothing to stop you from throwing your own IOException.
In other words, I need to inject - Invoke an exception while running, without affecting current code !
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.

Comments

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.