Results 1 to 6 of 6
  1. #1
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Unreported exception Exception; must be caught or declared to be thrown

    Trying to get input from a text field and use it in a method.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class TestingGUI extends JFrame{
    
       String input;
    	JPanel panelOne = new JPanel();
    	JTextField textField = new JTextField(30);
    	static JLabel labelOne = new JLabel("Welcome");
    	JLabel labelTwo = new JLabel("2");
    	JLabel labelThree = new JLabel("3");
    	JLabel labelFour = new JLabel("4");
    	    
    	public TestingGUI(){
    	
    		setTitle("Testing");
       	setVisible(true);
          setSize(600, 400);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
          panelOne.add(textField);
    		panelOne.add(labelOne);
    		panelOne.add(labelTwo);
    		panelOne.add(labelThree);
    		panelOne.add(labelFour);
    		
    		textField.addActionListener(new ActionListener(){
    		
    			public void actionPerformed(ActionEvent e){
    			
    				input = textField.getText();
    				getInput(input);
    				labelThree = labelTwo;
    				labelTwo = labelOne;
    				textField.setText("");
    			}
    		});
    		panelOne.setLayout(new BoxLayout(panelOne, BoxLayout.Y_AXIS));
    		add(panelOne);
    	}
    	
    	public static void getInput(String input) throws Exception{
          String example = "http://www.nasdaq.com/symbol/" + input;
          Document document = Jsoup.connect(example).get();
          Element price = document.getElementById("qwidget_lastsale");
          labelOne.setText(price.text());
    	}
    	
    	
    	public static void main(String[] args){
    		TestingGUI game = new TestingGUI();
    	}
    }
    The error is: TestingGUI.java:37: error: unreported exception Exception; must be caught or declared to be thrown
    getInput(input);
    ^

    please help... thanks :P

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Unreported exception Exception; must be caught or declared to be thrown

    Because your method signature includes a throws clause you must put the getInput invocation inside a try/catch block. However,
    nowhere do I see a throw statement in the method. So perhaps you can just remove the throws clause. However, if any of the code
    within the getInput method throws a checked exception, then you will need a try/catch block there.

    Regards,
    Jim
    Last edited by jim829; 05-11-2014 at 04:44 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default Re: Unreported exception Exception; must be caught or declared to be thrown

    Do any of the constructors or methods in the getInput() method throw an Exception?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Unreported exception Exception; must be caught or declared to be thrown

    Quote Originally Posted by mbschultz97 View Post
    The error is: TestingGUI.java:37: error: unreported exception Exception; must be caught or declared to be thrown
    getInput(input);
    ^
    As Jim replied, the getInput(String) method's signature includes a throws clause, i.e.,
    Java Code:
    public static void getInput(String input) throws Exception
    The throws clause is necessary because the get() method in
    Java Code:
    Document document = Jsoup.connect(example).get();
    can throw an IOException (as declared in get()'s method signature), and you do not have a try-catch block around this line of code.

    Since getInput(String)'s method signature includes the throws clause, the caller of this method will therefore need to try-catch the exception. You'll need, e.g.,
    Java Code:
    public void actionPerformed(ActionEvent e) {
    
        input = textField.getText();
        try {
            getInput(input);
        }
        catch (Exception e) {
            // exception handling code here
        }
        labelThree = labelTwo;
        labelTwo = labelOne;
        textField.setText("");
    }
    See Catching and Handling Exceptions (The Java™ Tutorials > Essential Classes > Exceptions) and its subtopics for further info on the way to do this properly.

    Note that it is good practice when using the throws clause to specify the most specific possible exception to be thrown. In this case, you should specify the getInput(String) method to throw IOException, and not Exception, which is the supertype of all exception types. This is because each exception type indicates a specific type of problem, and so the exception handling code for each type can be different. See How to Throw Exceptions (The Java™ Tutorials > Essential Classes > Exceptions) for further information.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Unreported exception Exception; must be caught or declared to be thrown

    Except (no pun intended) that the first sentence of my reply was not accurate. Specifying a throws clause does not mean a try/catch block is needed. I started a thread of my own on the topic.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: Unreported exception Exception; must be caught or declared to be thrown

    Quote Originally Posted by jashburn View Post
    As Jim replied, the getInput(String) method's signature includes a throws clause, i.e.,
    Java Code:
    public static void getInput(String input) throws Exception
    The throws clause is necessary because the get() method in
    Java Code:
    Document document = Jsoup.connect(example).get();
    can throw an IOException (as declared in get()'s method signature), and you do not have a try-catch block around this line of code.

    Since getInput(String)'s method signature includes the throws clause, the caller of this method will therefore need to try-catch the exception. You'll need, e.g.,
    Java Code:
    public void actionPerformed(ActionEvent e) {
    
        input = textField.getText();
        try {
            getInput(input);
        }
        catch (Exception e) {
            // exception handling code here
        }
        labelThree = labelTwo;
        labelTwo = labelOne;
        textField.setText("");
    }
    See Catching and Handling Exceptions (The Java™ Tutorials > Essential Classes > Exceptions) and its subtopics for further info on the way to do this properly.

    Note that it is good practice when using the throws clause to specify the most specific possible exception to be thrown. In this case, you should specify the getInput(String) method to throw IOException, and not Exception, which is the supertype of all exception types. This is because each exception type indicates a specific type of problem, and so the exception handling code for each type can be different. See How to Throw Exceptions (The Java™ Tutorials > Essential Classes > Exceptions) for further information.
    Hey man thanks for the help and explanation :D my code works now

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2012, 01:42 PM
  2. Replies: 2
    Last Post: 04-27-2012, 02:43 PM
  3. Replies: 7
    Last Post: 03-21-2012, 08:07 AM
  4. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  5. Replies: 5
    Last Post: 02-14-2011, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •