Home > Archive > Java Help > November 2007 > exception
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Art Cummings 2007-11-29, 10:14 pm |
| Good evening all,
I'm taking a java class and as one of the final assignments, we've got to
write a program that uses a gui with buttons. We've got to use a function
that adds records. I've got this being handled by a button but i'm getting
an error that I need a throws exception. As luck would have it, the
instructor never covered this aspect of Java but I did find an example on
google. The problem i'm having is understanding how to use it. Since my
button processes the code it seems like the exception handling needs to be
there. I don't know of another way to call the button other than using the
listener, that triggers the event when the button is pressed. At this
point, i'm stuck. Any insight about how to do this, is appreciated. This
is an introductory java class so.
The error message I get.
¼§ÏStudentAddWindow.java:92: exception java.io.IOException is never thrown
in body of corresponding try statement
private class addButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{ String name;
} catch (IOException x)
{
FileWriter swriter = new FileWriter("c:\\studname.txt",true);
JOptionPane.showMessageDialog(null,"The file could not be written.");
//name = StudentTextField.getText();
FileWriter swriter = new FileWriter("c:\\studname.txt",true);
//FileWriter gwriter = new FileWriter("c:\\grades.txt",true);
PrintWriter outputFile2 = new PrintWriter(gwriter);
//PrintWriter outputFile= new PrintWriter(swriter);
outputFile.println(name);
//outputFile2.println("0,0,0");
outputFile.close();
//outputFile2.close();
StudentTextField.setText("");
}
Thanks
Art
| |
| Eric Sosman 2007-11-29, 10:14 pm |
| Art Cummings wrote:
> [...]
> The error message I get.
> ¼§ÏStudentAddWindow.java:92: exception java.io.IOException is never thrown
> in body of corresponding try statement
>
> private class addButtonListener implements ActionListener
> {
>
> public void actionPerformed(ActionEvent e)
>
>
> {
>
> try
> { String name;
> } catch (IOException x)
> [...]
Well, okay, why do you thing `String name;' is going
to throw an IOException? The whole inside of the try block
does nothing at all -- it declares a variable `name' that
can refer to String objects, but never uses it and never
does anything -- so there's no way an IOException can ever
be thrown. Why are you trying to catch something that
cannot exist?
--
Eric Sosman
esosman@ieee-dot-org.invalid
| |
| Art Cummings 2007-11-30, 4:28 am |
| My apologies, the code for the throw was assumed. I've included it here.
Thanks
public void actionPerformed(ActionEvent e)
{
try
{
addingStudent();
}
catch (IOException x)
{
String name;
name = StudentTextField.getText();
FileWriter swriter = new FileWriter("c:\\studname.txt",true);
FileWriter gwriter = new FileWriter("c:\\grades.txt",true);
PrintWriter outputFile2 = new PrintWriter(gwriter);
PrintWriter outputFile= new PrintWriter(swriter);
outputFile.println(name);
outputFile2.println("0,0,0");
outputFile.close();
outputFile2.close();
StudentTextField.setText("");
}
"Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
news:KrKdney7gd1EGNLanZ2dnUVZ_hisnZ2d@co
mcast.com...
> Art Cummings wrote:
>
> Well, okay, why do you thing `String name;' is going
> to throw an IOException? The whole inside of the try block
> does nothing at all -- it declares a variable `name' that
> can refer to String objects, but never uses it and never
> does anything -- so there's no way an IOException can ever
> be thrown. Why are you trying to catch something that
> cannot exist?
>
> --
> Eric Sosman
> esosman@ieee-dot-org.invalid
| |
| Andrew Thompson 2007-11-30, 4:28 am |
| On Nov 30, 2:29 pm, "Art Cummings" <aikia...@gmail.com> wrote:
> Good evening all,
My evening is better when I do not need to chase
after multi-posters asking them to 'kindly refrain
from multi-posting in future'.
(X-post to c.l.j.g./h., w/ f-u to c.l.j.h. only)
--
Andrew T.
| |
| Art Cummings 2007-11-30, 4:28 am |
| my apologies,
any suggestions on the code problem?
"Andrew Thompson" <andrewthommo@gmail.com> wrote in message
news:9ed3f516-a2f0-43b0-bfd5-de7ec1f47802@a35g2000prf.googlegroups.com...
> On Nov 30, 2:29 pm, "Art Cummings" <aikia...@gmail.com> wrote:
>
> My evening is better when I do not need to chase
> after multi-posters asking them to 'kindly refrain
> from multi-posting in future'.
>
> (X-post to c.l.j.g./h., w/ f-u to c.l.j.h. only)
>
> --
> Andrew T.
| |
| Roedy Green 2007-11-30, 4:28 am |
| On Thu, 29 Nov 2007 22:29:34 -0500, "Art Cummings"
<aikiart7@gmail.com> wrote, quoted or indirectly quoted someone who
said :
>try
>{ String name;
>} catch (IOException x)
the pattern is
try {
code that does IO .
}
catch ( IOExeception e )
{
Code to deal with the IO screwing up.
}
see http://mindprod.com/jgloss/exception.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
| |
| Andrew Thompson 2007-11-30, 4:28 am |
| Art Cummings wrote:
>my apologies,
Sure (I'll take that to read "it won't happen again" which
is all that interests me).
>any suggestions on the code problem?
Roedy and Eric have offered some useful advice if you
understand how to use it - you might research their
suggestions further.
I also recommend posting SSCCE code, as it helps
people to see the exact problem, and avoids the need
(largely) to provide later snippets of code.
You can find more info. on the SSCCE here.
<http://www.physci.org/codes/sscce.html>
Another thing I recommend is to 'do as the locals do'
and post 'in-line with trimming' as myself, Eric and
Roedy have done, rather than 'top-post' replies.
--
Andrew Thompson
http://www.physci.org/
Message posted via http://www.javakb.com
| |
| RedGrittyBrick 2007-11-30, 8:14 am |
| Art Cummings wrote:
> My apologies, the code for the throw was assumed. I've included it here.
>
>
> public void actionPerformed(ActionEvent e)
> {
> try
> {
>
> addingStudent();
> }
> catch (IOException x)
>
> {
>
> }
>
>
Maybe its simpler to show how I think it should be done.
public void actionPerformed(ActionEvent e) {
// Dear tutor, grill student re spoonfed solution from usenet.
try {
String name = StudentTextField.getText();
FileWriter swriter = new FileWriter("c:\\studname.txt",true);
PrintWriter outputFile= new PrintWriter(swriter);
outputFile.println(name);
outputFile.close();
FileWriter gwriter = new FileWriter("c:\\grades.txt",true);
PrintWriter outputFile2 = new PrintWriter(gwriter);
outputFile2.println("0,0,0");
outputFile2.close();
StudentTextField.setText("");
} catch (IOException x) {
System.out.println("Unable to add student - " + x.getMessage());
JOptionPane.showMessageDialog(frame, x.getMessage, "AppName",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
Untested, caveat emptor.
| |
|
| RedGrittyBrick wrote:
> Maybe its simpler to show how I think it should be done.
>
> public void actionPerformed(ActionEvent e) {
> // Dear tutor, grill student re spoonfed solution from usenet.
> try {
> String name = StudentTextField.getText();
>
> FileWriter swriter = new FileWriter("c:\\studname.txt",true);
> PrintWriter outputFile= new PrintWriter(swriter);
> outputFile.println(name);
> outputFile.close();
>
> FileWriter gwriter = new FileWriter("c:\\grades.txt",true);
> PrintWriter outputFile2 = new PrintWriter(gwriter);
> outputFile2.println("0,0,0");
> outputFile2.close();
>
> StudentTextField.setText("");
>
> } catch (IOException x) {
> System.out.println("Unable to add student - " + x.getMessage());
> JOptionPane.showMessageDialog(frame, x.getMessage, "AppName",
> JOptionPane.ERROR_MESSAGE);
> System.exit(1);
> }
> }
>
>
> Untested, caveat emptor.
All that I/O - could end up being a lengthy process, ergo, a candidate to move
off the EDT.
--
Lew
| |
| rossum 2007-11-30, 8:14 am |
| On Thu, 29 Nov 2007 22:29:34 -0500, "Art Cummings"
<aikiart7@gmail.com> wrote:
>Good evening all,
>
>I'm taking a java class and as one of the final assignments, we've got to
>write a program that uses a gui with buttons. We've got to use a function
>that adds records. I've got this being handled by a button but i'm getting
>an error that I need a throws exception. As luck would have it, the
>instructor never covered this aspect of Java but I did find an example on
>google. The problem i'm having is understanding how to use it. Since my
>button processes the code it seems like the exception handling needs to be
>there. I don't know of another way to call the button other than using the
>listener, that triggers the event when the button is pressed. At this
>point, i'm stuck. Any insight about how to do this, is appreciated. This
>is an introductory java class so.
>
[snip code]
If you want to understand exceptions further then you can read the Sun
tutorial at:
http://java.sun.com/docs/books/tuto...ial/exceptions/
In general the Sun tutorials are worth looking at if there is a part
of Java you do not understand.
You could also try experimenting on your own with something like:
public class ExceptionTest {
public static void thisThrows() {
// Just throw an exception
throw new Exception("this process has failed because your " +
"computer is full of instant mashed potato.");
}
public static void main(String[] args) {
try {
thisThrows();
System.out.println("We are here.");
}
catch (Exception ex) {
System.out.println("Exception thrown: " +
ex.getMessage());
}
System.out.println("Finished.");
}
}
Yes, I know that will not compile, getting it to compile is part of
the learning process (yes, I used to be a teacher).
For extra credits, predict whether or not the "We are here" message
will be printed before you run the code.
rossum
| |
| Art Cummings 2007-11-30, 10:13 pm |
| Thanks red, I see that I had the code somewhat reveresed. It's alot clearer
now how to use the try statement.
Art Cummings
"RedGrittyBrick" <RedGrittyBrick@SpamWeary.foo> wrote in message
news:474fea92$0$8414$db0fefd9@news.zen.co.uk...
> Art Cummings wrote:
>
> Maybe its simpler to show how I think it should be done.
>
> public void actionPerformed(ActionEvent e) {
> // Dear tutor, grill student re spoonfed solution from usenet.
> try {
> String name = StudentTextField.getText();
>
> FileWriter swriter = new FileWriter("c:\\studname.txt",true);
> PrintWriter outputFile= new PrintWriter(swriter);
> outputFile.println(name);
> outputFile.close();
>
> FileWriter gwriter = new FileWriter("c:\\grades.txt",true);
> PrintWriter outputFile2 = new PrintWriter(gwriter);
> outputFile2.println("0,0,0");
> outputFile2.close();
>
> StudentTextField.setText("");
>
> } catch (IOException x) {
> System.out.println("Unable to add student - " + x.getMessage());
> JOptionPane.showMessageDialog(frame, x.getMessage, "AppName",
> JOptionPane.ERROR_MESSAGE);
> System.exit(1);
> }
> }
>
>
> Untested, caveat emptor.
| |
| Art Cummings 2007-11-30, 10:13 pm |
| Thanks Andrew
I've read the sscce doc and will comply in future post.
Art Cummings
"Andrew Thompson" <u32984@uwe> wrote in message news:7bfb0269f7f01@uwe...
> Art Cummings wrote:
>
> Sure (I'll take that to read "it won't happen again" which
> is all that interests me).
>
>
> Roedy and Eric have offered some useful advice if you
> understand how to use it - you might research their
> suggestions further.
>
> I also recommend posting SSCCE code, as it helps
> people to see the exact problem, and avoids the need
> (largely) to provide later snippets of code.
>
> You can find more info. on the SSCCE here.
> <http://www.physci.org/codes/sscce.html>
>
> Another thing I recommend is to 'do as the locals do'
> and post 'in-line with trimming' as myself, Eric and
> Roedy have done, rather than 'top-post' replies.
>
> --
> Andrew Thompson
> http://www.physci.org/
>
> Message posted via http://www.javakb.com
>
| |
| Art Cummings 2007-11-30, 10:13 pm |
| Thanks Roedy,
I'm starting to understand how to use try.
Art Cummings
"Roedy Green" <see_website@mindprod.com.invalid> wrote in message
news:56gvk35lca8ll0q37dg7eiq4ogbngf0nk2@
4ax.com...
> On Thu, 29 Nov 2007 22:29:34 -0500, "Art Cummings"
> <aikiart7@gmail.com> wrote, quoted or indirectly quoted someone who
> said :
>
>
> the pattern is
>
> try {
>
> code that does IO .
>
> }
>
> catch ( IOExeception e )
>
> {
> Code to deal with the IO screwing up.
>
> }
>
> see http://mindprod.com/jgloss/exception.html
> --
> Roedy Green Canadian Mind Products
> The Java Glossary
> http://mindprod.com
| |
| Art Cummings 2007-11-30, 10:13 pm |
| Thanks Rossum,
This was a good reference.
Art Cummings
"rossum" <rossum48@coldmail.com> wrote in message
news:q120l3hv74nvs07p2rt2dhbcl3ke1rl868@
4ax.com...
> On Thu, 29 Nov 2007 22:29:34 -0500, "Art Cummings"
> <aikiart7@gmail.com> wrote:
>
> [snip code]
>
> If you want to understand exceptions further then you can read the Sun
> tutorial at:
> http://java.sun.com/docs/books/tuto...ial/exceptions/
>
> In general the Sun tutorials are worth looking at if there is a part
> of Java you do not understand.
>
>
> You could also try experimenting on your own with something like:
>
> public class ExceptionTest {
>
> public static void thisThrows() {
> // Just throw an exception
> throw new Exception("this process has failed because your " +
> "computer is full of instant mashed potato.");
> }
>
>
> public static void main(String[] args) {
>
> try {
> thisThrows();
> System.out.println("We are here.");
> }
> catch (Exception ex) {
> System.out.println("Exception thrown: " +
> ex.getMessage());
> }
> System.out.println("Finished.");
> }
> }
>
> Yes, I know that will not compile, getting it to compile is part of
> the learning process (yes, I used to be a teacher).
>
> For extra credits, predict whether or not the "We are here" message
> will be printed before you run the code.
>
> rossum
>
| |
| RedGrittyBrick 2007-11-30, 10:13 pm |
| Lew wrote:
> RedGrittyBrick wrote:
[snip]
[color=darkred]
>
> All that I/O - could end up being a lengthy process, ergo, a candidate
> to move off the EDT.
>
Ooh yes ... let me dig myself a little deeper ...
Art, please avert your gaze ...
public void actionPerformed(ActionEvent e) {
// Blindly copying usenet may be hazardous to your education.
frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
final String name = StudentTextField.getText();
new SwingWorker<Void, Void>() {
private IOException pendingException;
@Override
protected Void doInBackground() { // not EDT
try {
FileWriter swriter = new FileWriter(
"c:\\studname.txt",true);
PrintWriter outputFile= new PrintWriter(swriter);
outputFile.println(name);
outputFile.close();
FileWriter gwriter = new FileWriter(
"c:\\grades.txt",true);
PrintWriter outputFile2 = new PrintWriter(gwriter);
outputFile2.println("0,0,0");
outputFile2.close();
} catch (IOException x) {
pendingException = x;
System.out.println("Unable to add student - "
+ pendingException.getMessage());
}
}
@Override
protected void done() { // on EDT
frame.setCursor(null);
if (pendingException == null) {
StudentTextField.setText("");
} else {
JOptionPane.showMessageDialog(
frame,
"Unable to add student - "
+ pendingException.getMessage,
"AppName",
JOptionPane.ERROR_MESSAGE);
System.exit(1); // or let 'em retry?
}
}
}.execute();
}
Untested, undoubtedly borken in many ways, ++caveat emptor--;
(Also IMO needs refactoring into smaller methods/classes to tame
indentation a bit)
| |
| Nigel Wade 2007-11-30, 10:13 pm |
| Lew wrote:
>
> All that I/O - could end up being a lengthy process, ergo, a candidate to move
> off the EDT.
and it might be a good idea to check if it actually succeeded. According to the
API PrintWriter.println does not throw any IOException if it fails.
I'll leave it as an exercise for the OP to figure out how to handle this
eventuality.
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
|
|
|
|
|