Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By superhaNds

Thread: Throws IOEXception must be caught or decalred to be thrown error??

  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Exclamation Throws IOEXception must be caught or decalred to be thrown error??

    Hi! I am trying to code a program that an imaginary supermarket can use to register stock, edit and stuff. At the minute I am working just on the entering stock. I am trying to get a menu that when you ask to enter new stock, and click on electronics, I want a list of electronics to come up as an option in a menu. However, at the minute I an getting an error saying getElectronics() must be caught or delcared to be thrown etc even though it is. How do I fix this? Also, when it was compiling, none of the electronics I have in a file .txt were listing as an option, just the name of the file. Anyone have any ideas??

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.math.*;
    import java.text.*;
    import javax.swing.*;
    public class Tesco
    {
    	static File file = new File("Electronics.txt");
    	public static void main(String[]args)
    	{
    		//Create the menu for the Tesco program to choose options.
    		String[] choices = {"1.Enter new stock.","2.Review current stock levels.","3.Edit stock.","0.Exit"};
    		Object option = JOptionPane.showInputDialog(null,"Please pick an option: ","Main Menu",1,null,choices,choices[0]);
    		if(option.equals("1.Enter new stock."))
    			enterStock();
    		else if (option.equals("2.Review current stock levels."))
    			reviewStock();
    		else if (option.equals("3.Edit stock."))
    			editStock();
    		else if (option.equals("0.Exit") || option == null)
    			JOptionPane.showMessageDialog(null,"The program will exit!","Exit",0);
    		else
    			JOptionPane.showMessageDialog(null,"The program will exit, possibly!","Exit",0);
    	}
    	
    	public static void enterStock()
    	{
    		JOptionPane.showMessageDialog(null,"Method to be entered!");
    		String[] choices = {"1.Electronics.","2.Fruits.","3.Meat.","4.Vegetables","0.Exit"};
    		Object option = JOptionPane.showInputDialog(null,"What type of stock would you like to enter?","Stock menu",1,null,choices,choices[0]);
    		if(option.equals("1.Electronics."))
    		{
    			String[] electronics = new String[getElectronics().size()];
    			electronics = getElectronics().toArray(electronics);
    			Object stockOption = JOptionPane.showInputDialog(null,"What type of stock would you like to enter?","Stock menu",1,null,electronics,electronics[0]);
    		}
    		
    		else if(option.equals("2.Fruits"))
    			JOptionPane.showMessageDialog(null,"Method to be entered!");
    		
    		else if(option.equals("3.Meat"))
    		JOptionPane.showMessageDialog(null,"Method to be entered!");
    		
    		else if(option.equals("4.Vegetables"))
    				JOptionPane.showMessageDialog(null,"Method to be entered!");
    		
    		else if(option.equals("0.Exit") || option == null)
    			JOptionPane.showMessageDialog(null,"The program will exit!","Exit",0);
    		else
    			JOptionPane.showMessageDialog(null,"The program will exit, possibly!","Exit",0);
    	}
    	
    	public static void reviewStock()
    	{
    		JOptionPane.showMessageDialog(null,"Method to be entered!");
    	}
    	
    	
    	public static void editStock()
    	{
    		JOptionPane.showMessageDialog(null,"Method to be entered!");
    	}
    	
    	public static ArrayList<String> getFruit(){
    		String aLine;
    		ArrayList<String> allFruits = new ArrayList<String>();
    		Scanner input = new Scanner ("Fruits.txt");
    		while(input.hasNext()){
    			aLine = input.nextLine();
    			allFruits.add(aLine);
    		}
    		return allFruits;
    	}
    	
    	public static ArrayList<String> getVeg(){
    		String aLine;
    		ArrayList<String> allVeg = new ArrayList<String>();
    		Scanner input = new Scanner ("Vegetables.txt");
    		while(input.hasNext()){
    			aLine = input.nextLine();
    			allVeg.add(aLine);
    		}
    		return allVeg;
    	}
    	
    	public static ArrayList<String> getMeat(){
    		String aLine;
    		ArrayList<String> allMeats = new ArrayList<String>();
    		Scanner input = new Scanner ("Meat.txt");
    		while(input.hasNext()){
    			aLine = input.nextLine();
    			allMeats.add(aLine);
    		}
    		return allMeats;
    	}
    	
    	public static ArrayList<String> getElectronics() throws IOException{
    		String aLine;
    		ArrayList<String> allElectronics = new ArrayList<String>();
    		Scanner input = new Scanner(file);
    		while(input.hasNext()){
    			aLine = input.nextLine();
    			allElectronics.add(aLine);
    		}
    		return allElectronics;
    	}
    	
    }

  2. #2
    ShadowWalker is offline Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    96
    Rep Power
    0

    Default Re: Throws IOEXception must be caught or decalred to be thrown error??

    this is the correct code :

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.math.*;
    import java.text.*;
    import javax.swing.*;
    public class Tesco
    {
        static File file = new File("Electronics.txt");
        public static void main(String[]args)
        {
            //Create the menu for the Tesco program to choose options.
            String[] choices = {"1.Enter new stock.","2.Review current stock levels.","3.Edit stock.","0.Exit"};
            Object option = JOptionPane.showInputDialog(null,"Please pick an option: ","Main Menu",1,null,choices,choices[0]);
            if(option.equals("1.Enter new stock."))
                enterStock();
            else if (option.equals("2.Review current stock levels."))
                reviewStock();
            else if (option.equals("3.Edit stock."))
                editStock();
            else if (option.equals("0.Exit") || option == null)
                JOptionPane.showMessageDialog(null,"The program will exit!","Exit",0);
            else
                JOptionPane.showMessageDialog(null,"The program will exit, possibly!","Exit",0);
        }
         
        public static void enterStock()
        {
            JOptionPane.showMessageDialog(null,"Method to be entered!");
            String[] choices = {"1.Electronics.","2.Fruits.","3.Meat.","4.Vegetables","0.Exit"};
            Object option = JOptionPane.showInputDialog(null,"What type of stock would you like to enter?","Stock menu",1,null,choices,choices[0]);
            if(option.equals("1.Electronics."))
            {
                String[] electronics = new String[getElectronics().size()];
                electronics = getElectronics().toArray(electronics);
                Object stockOption = JOptionPane.showInputDialog(null,"What type of stock would you like to enter?","Stock menu",1,null,electronics,electronics[0]);
            }
             
            else if(option.equals("2.Fruits"))
                JOptionPane.showMessageDialog(null,"Method to be entered!");
             
            else if(option.equals("3.Meat"))
            JOptionPane.showMessageDialog(null,"Method to be entered!");
             
            else if(option.equals("4.Vegetables"))
                    JOptionPane.showMessageDialog(null,"Method to be entered!");
             
            else if(option.equals("0.Exit") || option == null)
                JOptionPane.showMessageDialog(null,"The program will exit!","Exit",0);
            else
                JOptionPane.showMessageDialog(null,"The program will exit, possibly!","Exit",0);
        }
         
        public static void reviewStock()
        {
            JOptionPane.showMessageDialog(null,"Method to be entered!");
        }
         
         
        public static void editStock()
        {
            JOptionPane.showMessageDialog(null,"Method to be entered!");
        }
         
        public static ArrayList<String> getFruit(){
            String aLine;
            ArrayList<String> allFruits = new ArrayList<String>();
            Scanner input = new Scanner ("Fruits.txt");
            while(input.hasNext()){
                aLine = input.nextLine();
                allFruits.add(aLine);
            }
            return allFruits;
        }
         
        public static ArrayList<String> getVeg(){
            String aLine;
            ArrayList<String> allVeg = new ArrayList<String>();
            Scanner input = new Scanner ("Vegetables.txt");
            while(input.hasNext()){
                aLine = input.nextLine();
                allVeg.add(aLine);
            }
            return allVeg;
        }
         
        public static ArrayList<String> getMeat(){
            String aLine;
            ArrayList<String> allMeats = new ArrayList<String>();
            Scanner input = new Scanner ("Meat.txt");
            while(input.hasNext()){
                aLine = input.nextLine();
                allMeats.add(aLine);
            }
            return allMeats;
        }
         
        public static ArrayList<String> getElectronics(){
            String aLine;
            ArrayList<String> allElectronics = new ArrayList<String>();
            Scanner input = null;
            try{
            	input = new Scanner(file);
            }catch(Exception e){
            	System.out.println("File Not Found!");
            }
            while(input.hasNext()){
                aLine = input.nextLine();
                allElectronics.add(aLine);
            }
            return allElectronics;
        }
         
    }
    now let's talk about the error in your getElectronics() method.. you write that this method will throws IOException so that's means that everytime you call this method you should check if you throw any Exception.. os for example if you want to call this you should call it like this way :

    Java Code:
    try{
    	electronics = getElectronics().toArray(electronics);
    }catch(Exception e){
    	System.out.println("File used it in method GetElectornics not Found!");
    }
    but to avoid this .. you can just remove the throws IOException on the getElectronics and to do
    a standard check if this method throw any Exception you do it like this :

    Java Code:
     public static ArrayList<String> getElectronics() {
            String aLine;
            ArrayList<String> allElectronics = new ArrayList<String>();
            Scanner input = null; // you need this line because input maybe not have been initialised
    		try{
    			input = new Scanner(file); // load the file
    		}catch{
    			System.out.println("Can't find the File");
    			System.out.println(0); // to exit program or whatever (for example a return statement..)
    		}
            while(input.hasNext()){
                aLine = input.nextLine();
                allElectronics.add(aLine);
            }
            return allElectronics;
        }

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    232
    Rep Power
    1

    Default Re: Throws IOEXception must be caught or decalred to be thrown error??

    IOException is a checked exception (extends Exception) and thus you are forced to do something about it.
    Eventually you will either 'catch' it or 'throw' it.
    By declaring in the throws clause that the method throws a particular exception it means you pass up the exception and won't handle it (You can catch it and throw it manually however)
    So whenever you use the particular method you must handle the exception or pass it up the line again (throw it).
    So the error is basically unhandled exception, which is disallowed and in your case as ShadowWalker said whenever you call it, it must be in try -catch block to get through the compiler
    ShadowWalker likes this.

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2012, 01:42 PM
  2. Replies: 7
    Last Post: 03-21-2012, 08:07 AM
  3. throws IOException ?
    By usuf in forum New To Java
    Replies: 2
    Last Post: 06-10-2011, 12:35 PM
  4. char and throws java.io.IOException
    By blessed07 in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 07:05 PM
  5. change throws IOException
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 7
    Last Post: 11-23-2009, 08:39 AM

Posting Permissions

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