Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-08-2008, 04:38 AM
Member
 
Join Date: Mar 2008
Posts: 2
desmond5 is on a distinguished road
Trouble with factory method - unhandled exception type Exception
Hi!

I have a simple factory method:

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:

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

Best wishes, Desmond
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-08-2008, 07:41 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Quote:
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
Code:
public class FactoryTest { public static void main(String[] args) { // The ComputerFactory.create 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 }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Instantiation using an instance factory method Java Tip Java Tips 0 03-29-2008 01:35 PM
Need help on Exception Deon New To Java 5 01-26-2008 09:27 AM
Main method throwing specific Exception bugger New To Java 3 01-22-2008 04:08 AM
Main method with throws Exception bugger New To Java 3 01-07-2008 03:48 PM
Instantiation using an instance factory method JavaBean Java Tips 0 09-26-2007 09:25 PM


All times are GMT +3. The time now is 11:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org