Solved

SwingWorker in try/catch

Posted on 2011-03-13
7
608 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
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 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 125 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 26

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 125 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 26

Assisted Solution

by:dpearson
dpearson earned 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now