Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SwingWorker in try/catch

Posted on 2011-03-13
7
Medium Priority
?
617 Views
Last Modified: 2012-06-21
When I do this:

try {
outputStream = new PrintWriter(new FileWriter("myfile.txt");
outputStream.println("execute"); 
new ProgressTask().execute();
outputStream.close();
} 
catch (IOException e) {
e.printStackTrace();
}

Open in new window


ProgressTask.doInBackground() is not called. But when I move the call to execute() outside the try/catch:

try {
outputStream = new PrintWriter(new FileWriter("myfile.txt");
outputStream.println("execute"); 
} 
catch (IOException e) {
e.printStackTrace();
}
new ProgressTask().execute();
outputStream.close();

Open in new window


it is called. Why?
No exception is being thrown, so it is making it to new ProgressTask().execute(); in both cases.

private class ProgressTask extends SwingWorker<Void, Void> {
		
public Void doInBackground() {
	try {
		System.out.println("Sleeping for two secs...");
		Thread.sleep(2000);
	} catch (InterruptedException e) {
		/* ignore */
	}
	return null;
}

public void done() {
	jProgressBar.setIndeterminate(false);
}
}

Open in new window

0
Comment
Question by:allelopath
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 35122553
Can you give more complete code context please? Preferably something that's runnable that demos the problem
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 35122789
How can you have outputStream.close() not within the try/catch loop in the second case - wouldn't compiler caomplain?
0
 
LVL 28

Expert Comment

by:dpearson
ID: 35123434
This can't be the actual code you're running since this line:

outputStream = new PrintWriter(new FileWriter("myfile.txt");

clearly won't compile (it's in both examples and it missing a closing brace).

The only difference in the execution path for your two examples depends on whether there's an exception thrown or not.  So:
1) Either it is throwing an exception and you're just not seeing the output from the exception for some reason
or
2) This isn't the key part of the code and something else is causing the problem.

Doug
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 92

Expert Comment

by:objects
ID: 35123644
you don't need a SwingWorker to achieve that
thats what a Swing timer is for, far simpler
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 35123822
All you need is this:

               Timer timer = new Timer(2000, new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                        setProgressIndeterminate(true);
               });
               timer.setRepeats(false);
               timer.start();

And having it inside the try/catch will mean it will not get called if an exception is thrown opening/writing the file
0
 
LVL 1

Author Comment

by:allelopath
ID: 35123985
Thanks for the responses. Allow me to refine the question to this:
Is there something intrinsically incorrect with calling ProgressTask().execute(); from within a try/catch?
0
 
LVL 28

Assisted Solution

by:dpearson
dpearson earned 500 total points
ID: 35124014

Is there something intrinsically incorrect with calling ProgressTask().execute(); from within a try/catch?]

No - it's fine to call any code inside a try {} catch {} block.

Indeed most production applications run all code inside a top level try...catch block, so an exception which propagates to the top level doesn't shutdown the application.

Doug
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question