Unreported Exception Compiler Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnixuser
    New Member
    • Dec 2006
    • 80

    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
    Code:
    /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:
    Code:
     mi_File_Save.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //System.exit(0);
                    //final JFileChooser fc = new JFileChooser();
                    int returnVal = fc.showSaveDialog(Editor.this);
                    SaveOutput(returnVal);//this line right here is what kicks out the error
                    //String nfile;
                    
    
                    //final JFileChooser fc = new JFileChooser();
                   // int returnVal = fc.showOpenDialog(e);
                }
            });//not part of class examples
    And here is the method that is being called from the Actionlistener:

    Code:
    private void SaveOutput(int returnVal) throws IOException
        {
            try {
            String nfile;
            if(returnVal == JFileChooser.APPROVE_OPTION)
                    {
                        nfile = fc.getSelectedFile().toString();
                        String text = jtaNotes.getText();
                        FileWriter fw = new FileWriter(nfile);
                        fw.close();
                    }
            }
            catch(IOException SIOxcp)
            {
                JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
            }
                    
        }
    And here is the program in its entirety:
    Code:
    /*
     * Editor.java
     *
     * Created on February 13, 2007, 1:05 PM
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     */
    
    /**
     *
     * @author vontux
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    import java.io.*;//not part of class example code
    public class Editor extends JFrame {
        private JTextArea jtaNotes = new JTextArea(24,60);//it will only be able to scroll
        private JScrollPane jspNotes = new JScrollPane(jtaNotes);//pass component to the scroll area you want to be scrollable
        final JFileChooser fc = new JFileChooser();//not part of class code
        
        /** Creates a new instance of Editor */
        public Editor() throws java.io.IOException {
            setTitle("MyEditor");
            
            //adding the menus
            JMenuBar jmb = new JMenuBar();//where the JMenu's go
            
            JMenu m_file = new JMenu("File");
            JMenu m_Edit = new JMenu("Edit");
            JMenu m_Search = new JMenu("Search");
            JMenu m_Help = new JMenu("Help");
            
            JMenuItem mi_File_Exit = new JMenuItem("Exit");
            
            JMenuItem mi_File_Save = new JMenuItem("Save");
            
            JMenuItem mi_File_SaveAs = new JMenuItem("Save As");
            
            JMenuItem mi_File_Open = new JMenuItem("Open");
            mi_File_Exit.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                    
                }
            });
            
            mi_File_Save.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    //System.exit(0);
                    //final JFileChooser fc = new JFileChooser();
                    int returnVal = fc.showSaveDialog(Editor.this);
                    SaveOutput(returnVal);//this line right here is what kicks out the error
                    //String nfile;
                    
    
                    //final JFileChooser fc = new JFileChooser();
                   // int returnVal = fc.showOpenDialog(e);
                }
            });//not part of class examples
            m_file.add(mi_File_Open);//not part of class examples
            m_file.add(mi_File_Save);//not part of class examples
            m_file.add(mi_File_SaveAs);//not part of class examples
            m_file.add(mi_File_Exit);//adds a menu item to the specified JMenu object
            
            
            jmb.add(m_file);//these add the Menu's to the JMenu
            jmb.add(m_Edit);
            jmb.add( m_Search);
            jmb.add(m_Help);
            
            setJMenuBar( jmb );//adding the JMenuBar object initializing it I suppose
            
            setBackground(Color.GREEN);//sets background of the form
            
            jtaNotes.setBackground(Color.GREEN);//sets bg color of the jtaNotes Object
            jtaNotes.setFont( new Font("Times New Roman", Font.BOLD, 14));
            
            
            getContentPane().add(jspNotes, BorderLayout.CENTER);//getContentPane() is a container which gives access to other methods
            //setSize(300,400);//sets a default size for the window
            //pack();//autoadjusts based on the content
            
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);//allows you to close the jvm process by clicking the red x as opposed to simply hiding it
                }
            });
            pack();//autoadjusts based on the size of contents of it, in this case the size of the JScrollArea
            show();
        }
        public static void main(String[] args)
            throws java.io.IOException
        {
            try
            {
                new Editor();
            }
            catch(IOException IOxcp)
            {
                JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
            }
            
        }
        private void SaveOutput(int returnVal) throws IOException
        {
            try {
            String nfile;
            if(returnVal == JFileChooser.APPROVE_OPTION)
                    {
                        nfile = fc.getSelectedFile().toString();
                        String text = jtaNotes.getText();
                        FileWriter fw = new FileWriter(nfile);
                        fw.close();
                    }
            }
            catch(IOException SIOxcp)
            {
                JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
            }
                    
        }
        
    }
    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.
  • hirak1984
    Contributor
    • Jan 2007
    • 316

    #2
    First of all either delete the "throws IOException " line,
    or rewrite catch as
    Code:
    catch(Exception SIOxcp)
    both place IOException is not the syntax.Your errors will get resolved,hopefu lly.
    Originally posted by cnixuser
    Code:
    /home/vontux/Swing2/src/Editor.java:55: unreported exception java.io.IOException; must be caught or declared to be thrown
    Code:
    private void SaveOutput(int returnVal) throws IOException
    {
    try {
    String nfile;
    if(returnVal == JFileChooser.APPROVE_OPTION)
    {
    nfile = fc.getSelectedFile().toString();
    String text = jtaNotes.getText();
    FileWriter fw = new FileWriter(nfile);
    fw.close();
    }
    }
    catch(IOException SIOxcp)
    {
    JOptionPane.showMessageDialog(null,"Error","an IOException was Thrown",JOptionPane.ERROR_MESSAGE);
    }
     
    }
    .
    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.
    Originally posted by cnixuser
    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.

    Comment

    • cnixuser
      New Member
      • Dec 2006
      • 80

      #3
      Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOExcep tion" statements override the actionListener?
      Last edited by cnixuser; Feb 14 '07, 05:14 AM. Reason: restating the question more clearly

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by cnixuser
        Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOExcep tion" 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...whatev er exception. Do not use both.

        Comment

        • hirak1984
          Contributor
          • Jan 2007
          • 316

          #5
          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
          Code:
          implements actionListener
          at appropriate places.
          Originally posted by cnixuser
          Thank you, your deletion and modification suggestions solved the problem. Just out of curiosity, how do the "throws java.io.IOExcep tion" statements override the actionListener?

          Comment

          Working...