ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97
Rep:
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:
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.
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.
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97
Original Poster
Rep:
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.
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.
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97
Original Poster
Rep:
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?
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.
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.