Results 1 to 2 of 2
  1. #1
    desmond5 is offline Member
    Join Date
    Mar 2008
    Posts
    2
    Rep Power
    0

    Default Trouble with factory method - unhandled exception type Exception

    Hi!

    I have a simple factory method:

    Java Code:
    public final class ComputerFactory {
    	
    	public static Computer create(ComputerType type) throws Exception
    	{
    			
    		switch(type)
    		{
    			case PC:
    				return new CompPC();
    			case Mac:
    				return new CompMac();
    			case Sparc:
    				return new CompSparc();
    			case Itanium:
    				return new CompItanium();		
    		}
    		
    		throw new Exception("No such computer: " + type );		
     
    	}
     
    }
    ComputerType is a enum with 4 items in it (all listen in the switch block).

    When I try to create new Computers in another class with the factory method I get an error says: unhandled exception type Excepton:

    Java Code:
    comps.add(ComputerFactory.create(ComputerType.PC));
    What's wrong with the code ?

    Best wishes, Desmond

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    C:\jexp>javac factorytest.java
    factorytest.java:3: unreported exception java.lang.Exception; must be caught or declared to be thrown
    String mac = ComputerFactory.create(ComputerType.Mac);
    ^
    1 error
    Java Code:
    public class FactoryTest {
        public static void main(String[] args) {
            // The ComputerFactory.[i]create[/i] method declares
            // that it throws an Exception. So the caller
            // must be prepared to catch it.
            String mac = null;
            try {
                mac = ComputerFactory.create(ComputerType.Mac);
            } catch(Exception e) {
                System.out.println(e.getClass().getName() + ": " +
                                   e.getMessage());
            }
            System.out.println("mac = " + mac);
        }
    }
    
    final class ComputerFactory {
        public static String create(ComputerType type) throws Exception {
            switch(type) {
                case PC:      return "new CompPC";
                case Mac:     return "new CompMac";
                case Sparc:   return "new CompSparc";
                case Itanium: return "new CompItanium";		
            }
            throw new Exception("No such computer: " + type );		
        }
    }
    
    enum ComputerType { PC, Mac, Sparc, Itanium }

Similar Threads

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 05:46 PM
  2. Main method throwing specific Exception
    By bugger in forum New To Java
    Replies: 5
    Last Post: 05-13-2009, 02:34 PM
  3. Instantiation using an instance factory method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 12:35 PM
  4. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM
  5. Instantiation using an instance factory method
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:25 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
  •