Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

static try method consolidation

Thread view: 
thufir - 07 May 2008 21:34 GMT
Working from the Sun tutorial, to a degree.  How can I combine tryIt()
and scanXan() into one method?  Of course, it's silly to pass the String,
but what I mean is the try/catch and exception handling:

thufir@arrakis:~/bcit-comp2611-lab4$ cat src/a00720398/util/
DataException.java
package a00720398.util;

import a00720398.data.*;
import a00720398.util.*;
import a00720398.labs.*;

import java.util.*;
import java.io.*;

public abstract class DataException {

       public static void tryIt(){
               String[] foo = {"a","b","c"};

               try{
                       scanXan(foo);
               } catch (Exception e) {
                       System.out.println(e.getMessage());
                       e.printStackTrace();
               }

       }

   public static void scanXan(String[] args) throws IOException {
       Scanner s = null;
       try {
           s = new Scanner(new BufferedReader(new FileReader
("xanadu.txt")));

           while (s.hasNext()) {
               System.out.println(s.next());
           }
       } finally {
           if (s != null) {
               s.close();
           }
       }
   }
}
thufir@arrakis:~/bcit-comp2611-lab4$

thanks,

Thufir
Eric Sosman - 07 May 2008 22:28 GMT
> Working from the Sun tutorial, to a degree.  How can I combine tryIt()
> and scanXan() into one method?  Of course, it's silly to pass the String,
[quoted text clipped - 41 lines]
>     }
> }

    Approach #1: Snip out the entire inside of scanXan() and
paste it in place of the method call, renaming foo to args or
args to foo (or getting rid of both).

    Approach #2: As above, but consolidate the nested try/catch
structure from

    try {
       ...
       try {
           ...
       }
       finally {
           ...
       }
    } catch (Exception e) {
       ...
    }

to the simpler

    try {
       ...
    } catch (Exception e) {
       ...
    }
    finally {
       ...
    }

Signature

Eric.Sosman@sun.com

thufir - 07 May 2008 22:29 GMT
> Working from the Sun tutorial, to a degree.  How can I combine tryIt()
> and scanXan() into one method?  Of course, it's silly to pass the
> String, but what I mean is the try/catch and exception handling:

Oh, it was because I had the method itself throwing an exception!  

It at least compiles and runs, but is it "ok"?

thufir@arrakis:~/bcit-comp2611-lab4$
thufir@arrakis:~/bcit-comp2611-lab4$ cat src/a00720398/util/
DataException.java
package a00720398.util;

import a00720398.data.*;
import a00720398.util.*;
import a00720398.labs.*;

import java.util.*;
import java.io.*;

public abstract class DataException {

   public static void scanXan() {

               Scanner s = null;

               try {
                       s = new Scanner(new BufferedReader(new FileReader
("guests.txt")));
                       while (s.hasNext()) {
                               System.out.println(s.next());
                       }
               } catch (IOException e) {
                       System.out.println(e.getMessage());
                       e.printStackTrace();
               } finally {
                       if (s != null) {
                               s.close();
                       }
               }
       }
}
thufir@arrakis:~/bcit-comp2611-lab4$

thanks,

Thufir


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.