Solved

SwingWorker in try/catch

Posted on 2011-03-13
7
616 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 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 28

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

615 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