Solved

SwingWorker in try/catch

Posted on 2011-03-13
7
614 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 27

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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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 27

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 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