473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Unreported Exception Compiler Error

Hello,

I am posting reguarding some modifications I made to a text editor program that I wrote in my java class at my school today. The file I was modifying is a very simple java text editor with a "JMenuBar" object, with "JMenu" objects, such as "File" with "JMenuItem" objects such as "Save". The Modifications that I am attempting to make to the program are designed to give actual functionality to the "JMenuItems", starting with the "Save" object. I am implementing the functionality with ActionListener classes. I have run into an error that I am unsure how to correct while I was attempting to set up the FileWriter code I was planning to use to implement "Save". When I attempt to compile, I get the following error
Expand|Select|Wrap|Line Numbers
  1. /home/vontux/Swing2/src/Editor.java:55: unreported exception java.io.IOException; must be caught or declared to be thrown
Here is the portion of code that throws the compiler error:
Expand|Select|Wrap|Line Numbers
  1.  mi_File_Save.addActionListener( new ActionListener() {
  2.             public void actionPerformed(ActionEvent e) {
  3.                 //System.exit(0);
  4.                 //final JFileChooser fc = new JFileChooser();
  5.                 int returnVal = fc.showSaveDialog(Editor.this);
  6.                 SaveOutput(returnVal);//this line right here is what kicks out the error
  7.                 //String nfile;
  8.  
  9.  
  10.                 //final JFileChooser fc = new JFileChooser();
  11.                // int returnVal = fc.showOpenDialog(e);
  12.             }
  13.         });//not part of class examples
And here is the method that is being called from the Actionlistener:

Expand|Select|Wrap|Line Numbers
  1. private void SaveOutput(int returnVal) throws IOException
  2.     {
  3.         try {
  4.         String nfile;
  5.         if(returnVal == JFileChooser.APPROVE_OPTION)
  6.                 {
  7.                     nfile = fc.getSelectedFile().toString();
  8.                     String text = jtaNotes.getText();
  9.                     FileWriter fw = new FileWriter(nfile);
  10.                     fw.close();
  11.                 }
  12.         }
  13.         catch(IOException SIOxcp)
  14.         {
  15.             JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
  16.         }
  17.  
  18.     }
And here is the program in its entirety:
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * Editor.java
  3.  *
  4.  * Created on February 13, 2007, 1:05 PM
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9.  
  10. /**
  11.  *
  12.  * @author vontux
  13.  */
  14. import java.awt.*;
  15. import java.awt.event.*;
  16. import javax.swing.*;
  17.  
  18. import java.io.*;//not part of class example code
  19. public class Editor extends JFrame {
  20.     private JTextArea jtaNotes = new JTextArea(24,60);//it will only be able to scroll
  21.     private JScrollPane jspNotes = new JScrollPane(jtaNotes);//pass component to the scroll area you want to be scrollable
  22.     final JFileChooser fc = new JFileChooser();//not part of class code
  23.  
  24.     /** Creates a new instance of Editor */
  25.     public Editor() throws java.io.IOException {
  26.         setTitle("MyEditor");
  27.  
  28.         //adding the menus
  29.         JMenuBar jmb = new JMenuBar();//where the JMenu's go
  30.  
  31.         JMenu m_file = new JMenu("File");
  32.         JMenu m_Edit = new JMenu("Edit");
  33.         JMenu m_Search = new JMenu("Search");
  34.         JMenu m_Help = new JMenu("Help");
  35.  
  36.         JMenuItem mi_File_Exit = new JMenuItem("Exit");
  37.  
  38.         JMenuItem mi_File_Save = new JMenuItem("Save");
  39.  
  40.         JMenuItem mi_File_SaveAs = new JMenuItem("Save As");
  41.  
  42.         JMenuItem mi_File_Open = new JMenuItem("Open");
  43.         mi_File_Exit.addActionListener( new ActionListener() {
  44.             public void actionPerformed(ActionEvent e) {
  45.                 System.exit(0);
  46.  
  47.             }
  48.         });
  49.  
  50.         mi_File_Save.addActionListener( new ActionListener() {
  51.             public void actionPerformed(ActionEvent e) {
  52.                 //System.exit(0);
  53.                 //final JFileChooser fc = new JFileChooser();
  54.                 int returnVal = fc.showSaveDialog(Editor.this);
  55.                 SaveOutput(returnVal);//this line right here is what kicks out the error
  56.                 //String nfile;
  57.  
  58.  
  59.                 //final JFileChooser fc = new JFileChooser();
  60.                // int returnVal = fc.showOpenDialog(e);
  61.             }
  62.         });//not part of class examples
  63.         m_file.add(mi_File_Open);//not part of class examples
  64.         m_file.add(mi_File_Save);//not part of class examples
  65.         m_file.add(mi_File_SaveAs);//not part of class examples
  66.         m_file.add(mi_File_Exit);//adds a menu item to the specified JMenu object
  67.  
  68.  
  69.         jmb.add(m_file);//these add the Menu's to the JMenu
  70.         jmb.add(m_Edit);
  71.         jmb.add( m_Search);
  72.         jmb.add(m_Help);
  73.  
  74.         setJMenuBar( jmb );//adding the JMenuBar object initializing it I suppose
  75.  
  76.         setBackground(Color.GREEN);//sets background of the form
  77.  
  78.         jtaNotes.setBackground(Color.GREEN);//sets bg color of the jtaNotes Object
  79.         jtaNotes.setFont( new Font("Times New Roman", Font.BOLD, 14));
  80.  
  81.  
  82.         getContentPane().add(jspNotes, BorderLayout.CENTER);//getContentPane() is a container which gives access to other methods
  83.         //setSize(300,400);//sets a default size for the window
  84.         //pack();//autoadjusts based on the content
  85.  
  86.         addWindowListener(new WindowAdapter() {
  87.             public void windowClosing(WindowEvent e) {
  88.                 System.exit(0);//allows you to close the jvm process by clicking the red x as opposed to simply hiding it
  89.             }
  90.         });
  91.         pack();//autoadjusts based on the size of contents of it, in this case the size of the JScrollArea
  92.         show();
  93.     }
  94.     public static void main(String[] args)
  95.         throws java.io.IOException
  96.     {
  97.         try
  98.         {
  99.             new Editor();
  100.         }
  101.         catch(IOException IOxcp)
  102.         {
  103.             JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
  104.         }
  105.  
  106.     }
  107.     private void SaveOutput(int returnVal) throws IOException
  108.     {
  109.         try {
  110.         String nfile;
  111.         if(returnVal == JFileChooser.APPROVE_OPTION)
  112.                 {
  113.                     nfile = fc.getSelectedFile().toString();
  114.                     String text = jtaNotes.getText();
  115.                     FileWriter fw = new FileWriter(nfile);
  116.                     fw.close();
  117.                 }
  118.         }
  119.         catch(IOException SIOxcp)
  120.         {
  121.             JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
  122.         }
  123.  
  124.     }
  125.  
  126. }
Please ignore the comments reguarding my academic class, they are meant for my Professor and other students reading my code. Any help that could be provided on this problem would be greatly appreciated, because I am quite honestly stumped, as whenever I try to apply the "throws IOException" statement directly to the actionListener, there is a compiler error, I thought by applying it to the method, I would be eliminating the problem. So thanks in advance to anyone who helps me out here.
Feb 14 '07 #1
4 3619
hirak1984
316 100+
First of all either delete the "throws IOException " line,
or rewrite catch as
Expand|Select|Wrap|Line Numbers
  1. catch(Exception SIOxcp)
both place IOException is not the syntax.Your errors will get resolved,hopefully.
Expand|Select|Wrap|Line Numbers
  1. /home/vontux/Swing2/src/Editor.java:55: unreported exception java.io.IOException; must be caught or declared to be thrown
Expand|Select|Wrap|Line Numbers
  1. private void SaveOutput(int returnVal) throws IOException
  2. {
  3. try {
  4. String nfile;
  5. if(returnVal == JFileChooser.APPROVE_OPTION)
  6. {
  7. nfile = fc.getSelectedFile().toString();
  8. String text = jtaNotes.getText();
  9. FileWriter fw = new FileWriter(nfile);
  10. fw.close();
  11. }
  12. }
  13. catch(IOException SIOxcp)
  14. {
  15. JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
  16. }
  17.  
  18. }
.
dont try to add any throws after actionListener.Because you are overriding the function,so you cannot change the signature according to your need.That is not petmitted.
whenever I try to apply the "throws IOException" statement directly to the actionListener, there is a compiler error, I thought by applying it to the method, I would be eliminating the problem. So thanks in advance to anyone who helps me out here.
Feb 14 '07 #2
Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOException" statements override the actionListener?
Feb 14 '07 #3
r035198x
13,262 8TB
Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOException" statements override the actionListener?
The throws clause does not override anything. When writing code that can throw an exception which you want to handle, either put that code in a try block and catch the exception or declare that block of code as throws...whatever exception. Do not use both.
Feb 14 '07 #4
hirak1984
316 100+
it does not override actionListener.

here you are overriding actionListener when you are using it in your code.
actionListener is defined in an interface which you access by writing
Expand|Select|Wrap|Line Numbers
  1. implements actionListener
  2.  
at appropriate places.
Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOException" statements override the actionListener?
Feb 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
2
by: Steph | last post by:
I am working on a Windows Forms App. I have attached a standard UnhandledExceptionEventHandler to the current domain to catch unexpected errors. In my application, when I trigger an unhandled...
1
by: Pardhasaradhy | last post by:
Hi, Please see the following error and revert back as early as possible. I am getting this once I request for the asp.net page. Server Error in '/tanishq' Application. Timed out waiting for...
0
by: Marco Viana | last post by:
Hi, I'm developing an ASP.NET application with Visual Studio .NET 2003 in a Win XP Professional, .NET Framework 1.1 and IIS 5.1 computer with all the lattest patches. When testing a page...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
7
by: shana07 | last post by:
I have unreported exceptions as below: unreported exception java.lang.Exception; must be caught or declared to be thrown Euclid euc = new Euclid(); ...
11
by: George2 | last post by:
Hello everyone, How do you understand the Bjarne's comments about exception specification? Especially, "not required to be checked across compilation-unit" and "violations will not be caught...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.