Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: throwing ex...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 9 Topic 16067 of 16477
Post > Topic >>

Re: throwing exceptions on instantiation

by Knute Johnson <nospam@[EMAIL PROTECTED] > May 8, 2008 at 10:04 PM

thufir wrote:
> The makeGuest is working fine, probably because the data is fine.  If
the 
> constructor of Guest throws a DataException, though, I wouldn't want 
> makeGuest to return a new Guest!  I'm not quite sure what it should do, 
> if not that, though...
> 
> Should I try makeGuest first?  Would it make sense to have a
tryMakeGuest
> () method?
> 
> 
> 
> thufir@[EMAIL PROTECTED]
 
> thufir@[EMAIL PROTECTED]
 
> thufir@[EMAIL PROTECTED]
 cat src/a00720398/util/
> FileUtil.java
> /**
>  * FileUtil.java
>  */
> 
> package a00720398.util;
> 
> im****t java.util.*;
> im****t java.io.*;
> im****t a00720398.data.*;
> 
> public abstract class FileUtil {
> 
>         public static Guest makeGuest(List<String> guestData)throws 
> DataException{
>                 try{
>                         String[] data = guestData.toArray(new String
> [guestData.size()]);
>                         Guest guest = new Guest(data);
>                         return guest;
>                 } catch (DataException e) {
>                         e.printStackTrace();
>                 }
>                 return new Guest();
>         }
> 
>         public static List<Guest> loadGuests(){
>                 List<Guest> guests = new ArrayList<Guest>();
> 
> 
>           File file = new File("Guests.txt");
> 
>                 try {
>                         Scanner scanner = new Scanner(file);
>                         while (scanner.hasNextLine()) {
>                                 List<String> guestData = new 
> ArrayList<String>();
>                                 String line = scanner.nextLine();
>                                 Scanner lineScanner = new Scanner(line);
>                                 lineScanner.useDelimiter("\t");
>                                 while (lineScanner.hasNext()) {
>                                         String part =
lineScanner.next();
>                                         guestData.add(part);
>                                         if (!lineScanner.hasNext()) {
>                                                 try{
>                                                         Guest guest = 
> makeGuest(guestData);
>                                                        
guests.add(guest);
>                                                 } catch (DataException
e){
>                                                        
e.printStackTrace
> ();
>                                                 }
>                                         }
>                                 }
>                         }
>                 }catch (FileNotFoundException e) {
>                         e.printStackTrace();
>                 }
>                 return guests;
>         }
> 
> 
> 
> }
> thufir@[EMAIL PROTECTED]
 
> 

Just re-throw the exception.

-- 

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 




 9 Posts in Topic:
throwing exceptions on instantiation
thufir <hawat.thufir@[  2008-05-09 04:06:49 
Re: throwing exceptions on instantiation
Knute Johnson <nospam@  2008-05-08 22:04:19 
Re: throwing exceptions on instantiation
thufir <hawat.thufir@[  2008-05-09 09:31:30 
Re: throwing exceptions on instantiation
Knute Johnson <nospam@  2008-05-09 09:15:52 
Re: throwing exceptions on instantiation
Daniel Pitts <newsgrou  2008-05-09 07:55:17 
Re: throwing exceptions on instantiation
thufir <hawat.thufir@[  2008-05-10 07:13:22 
Re: throwing exceptions on instantiation
Lew <lew@[EMAIL PROTEC  2008-05-10 08:24:08 
Re: throwing exceptions on instantiation
Roedy Green <see_websi  2008-05-10 13:03:09 
Re: throwing exceptions on instantiation
Lew <lew@[EMAIL PROTEC  2008-05-10 09:52:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 12:27:54 CDT 2008.