LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2006, 10:57 AM   #1
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
Trouble with an IOException


Hi,
I'm having a bit of trouble with IOExceptions in java.
I'm a begginer and have not come across exceptions before. I have created a method which returns a panel with an image in it, see below:

Quote:
private JPanel displayImage(String path) throws IOException
{
File file = new File(path);
BufferedImage image = ImageIO.read(file);
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel = new JPanel();
panel.add(label);
return panel;
}
I want to call this method when a button is pushed that recieves this panel and adds it to another panel in my Frame.
The idea is that there are several buttons each relating to an image of the same name and that pushing each one will display the related image in the same place every time. This is what I have tried:

Quote:
class labelListener implements ActionListener {
public void actionPerformed(ActionEvent e)
{
String selected = e.getActionCommand();
eastPanel.add(displayImage(selected));
eastPanel.repaint();
addBar.setText(selected);
}
However, when compiling this code I get an error saying 'unreported exception java.io.IOException; must be caught or declared to be thrown. I have tried to throw it the same was as the method does but it doesn't like it.
How can I make it work?
Thanks for any help given.
 
Old 12-08-2006, 11:32 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Java wants you to be "safe" and explicitly deal with exceptions. So you either need to state that your function can throw an IOException, or you need to catch and deal with it yourself. To the latter, take your first code snippet and instead write:
Code:
BufferedImage image;
try {
  image = ImageIO.read(file);
}
catch (IOException e) {
  System.err.println(e.toString());
  // put handler code here
}
JLabel label = new JLabel(new ImageIcon(image));
You'll have to fill in the handling code in case the file specified doesn't exist or can't be read.
 
Old 12-08-2006, 12:08 PM   #3
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Original Poster
Rep: Reputation: 15
Thanks but I have to iterate that I really am a begginner. I don't even know what an exception is and can't see how the code you've posted can replace my code. It doesn't seem to do the same thing at all. I don't know what 'handling code' is. If possible I wan't to be able to ignore any exceptions (whatever they are) and have the program do nothing with them, or anything, just so it works.
 
Old 12-08-2006, 12:17 PM   #4
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Quote:
Originally Posted by ChrisScott
Thanks but I have to iterate that I really am a begginner. I don't even know what an exception is and can't see how the code you've posted can replace my code. It doesn't seem to do the same thing at all. I don't know what 'handling code' is. If possible I wan't to be able to ignore any exceptions (whatever they are) and have the program do nothing with them, or anything, just so it works.
You can't just "ignore" exceptions. Think something like this: You want to open a file and write to it, but you don't have permission to write that file. If you try to modify that file, it will throw an exception. In java, when doing operations that can throw an exception, you need to catch them (either by placing between a try/catch statement or adding a throws declarions, just like taylor_venable stated.

I've a few suggestions:

- Use Eclipse IDE. It will give you hints all the time about operations that can throw expcetion and even nicely put them into try/catch clauses or adding a throws declaration. It really is neat.

- Start slower. Much slower. If you are new to Java, you should really avoid doing more "advanced" operations with files and GUI.

- If you post your whole code, we could try to fix it right away ^_^;;

Regards!

Last edited by Mega Man X; 12-08-2006 at 12:19 PM.
 
Old 12-08-2006, 12:31 PM   #5
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Original Poster
Rep: Reputation: 15
Hi,
I really do appreciate the help, but I still don't know what an exception is, just why they are created. I have to implement this action as part of a University assignment so unfortunately I can't start slower. I have tried throwing the exception but it doesn't work. I've heard about try, catch before but have absolutely no idea how to implement it.
I could post my entire code but it is very long and all the relevant is already posted at the start of this thread. By 'ignore' the exception, I mean catch it (however that is done) and take no action....any suggestions?
 
Old 12-08-2006, 12:34 PM   #6
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Original Poster
Rep: Reputation: 15
this is what i currently have
Quote:
private JPanel displayImage(String path)
{
File file = new File(path);
BufferedImage image;
try {
image = ImageIO.read(file);
}
catch (IOException e) {
System.err.println(e.toString());
}
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel = new JPanel();
panel.add(label);
return panel;
}
but compiling results in the error:
variable image might not have been initialized.
 
Old 12-08-2006, 12:38 PM   #7
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Try initializing it with a null:

Code:
BufferedImage image = null;
at the 4th line of your above code...
 
Old 12-08-2006, 12:42 PM   #8
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Hmmm... your project looks a lot similar to the one I did when starting out with Java. You may want to check it out:

http://www.linuxquestions.org/questi...d.php?t=141754
 
Old 12-08-2006, 01:01 PM   #9
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Original Poster
Rep: Reputation: 15
Success!!
I have made the first major step towards getting my programme running as it should.
Thanks a lot to everyone who posted responses. Cheers.
 
Old 12-08-2006, 01:42 PM   #10
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Nice!

Congratulations mate ^_^;;
 
Old 12-10-2006, 01:24 AM   #11
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Exceptions are really easy to understand one you understand the concept. It's similar to a run-time error in C and C++.

The conceptual difference between error handling and exception handling is that with error-handling, you know a certain situation will lead to an error and try and code around it (e.g. you try and avoid division by zero by checking the denominator value before you perform the operation).

In exception handling, you might come across a predictable or unpredictable situation similar to division by zero, but instead of handling it *before* the fault occurs (you might not be able to predict the cause of error in some cases) you handle it *after* the error occurs and hence "catch" it. In Java you cannot have "unhandled" exceptions in many cases and you have to use an exception catching mechanism in the code.

Hope that explains what exceptions are - they are unexpected errors the checking of which cannot usually be handled in the normal way in the program logic.
 
Old 12-10-2006, 03:51 PM   #12
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
IMHO the best features of exceptions as objects is that they can concisely contain the reasons for and data relevant to the error, and that they can be easily passed along as the stack unwinds.

From a certain standpoint, I'm not sure I fully agree with the difference between exceptions and "error handling" -- it seems to me that you're just passing the buck off to some lower level of the system, rather than having to deal with it yourself. For example, if you try to take element six from an array of length three, the virtual machine checks first before it accesses memory. Or in another example, the OS checks in advance to make sure you have the permission to open a file. But with exceptions you don't in your code have to check (and as you say, it would not be possible in this latter situation for you to accurately check, what with the possibility of the filesystem changing after you make your check).

Edit starts here:

And actually, if you compare Java's exceptions to the way that system calls are made from C programs, they're pretty similar. If you try to fopen() a file in C, you get the result after you've tried to open the file, not before. Zero is success, otherwise it failed. Again, the reason is because you can't accurately check it yourself, you need the OS to do it for you. Not really after the fact, because the file's not really been opened yet. But from the programmer's perspective, it happens after the attempt has been made. The only difference here between C and Java is that Java gives you a nicely-wrapped object to handle, which carries its meaning with it. C, on the other hand, gives you an integral error code which you can look up yourself if you're interested.

Last edited by taylor_venable; 12-10-2006 at 03:58 PM. Reason: Had another idea to add.
 
Old 12-10-2006, 09:11 PM   #13
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
Thanks taylor. Let me just make a clarification to my above statement.

The difference that I think between error and exception handling is quite simply this. When you do error-handling in code, the error states are either fully defined by the calling function or the situation where the error occurs is easily predictable and can be handled by coding conditionals to work around the situation.

In exception handling, you may or may not know the error situation and a block of code might lead to a single or several unsatisfactory results which *might* result in abnormal program termination even, so you "catch" that before the system does and handle it satisfactorily. So exceptions can be "caught" while errors are "handled" in code.

For error handling, you definitely need a conditional situation (if Flag==TRUE) etc. and so you depend on the calling function to provide you with the necessary inputs. In the exception mechanism, the advantage is that you don't need a conditional situation and the try...catch mechanism will handle every exception situation within the block of code (provided you catch the different objects or maybe use a generic exception object).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble with SCSI, Trouble with Sata humbletech99 Linux - Hardware 0 05-24-2006 04:05 AM
Java IOException: Too many open files naijaguy Programming 0 02-01-2005 05:20 PM
Java "throws IOException" problem Nylex Programming 5 01-30-2004 08:49 PM
trouble ahead, trouble behind....trouble with mplayer Goonie Linux - Software 3 07-02-2003 02:29 AM
Kernel Trouble (Not actually trouble though) chem1 Linux - General 4 10-01-2002 01:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration