Solved

SwingWorker in try/catch

Posted on 2011-03-13
7
615 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

751 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