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: static try ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 16058 of 16590
Post > Topic >>

Re: static try method consolidation

by Eric Sosman <Eric.Sosman@[EMAIL PROTECTED] > May 7, 2008 at 05:28 PM

thufir wrote:
> 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@[EMAIL PROTECTED]
 cat src/a00720398/util/
> DataException.java
> package a00720398.util;
> 
> 
> im****t a00720398.data.*;
> im****t a00720398.util.*;
> im****t a00720398.labs.*;
> 
> 
> im****t java.util.*;
> im****t 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();
>             }
>         }
>     }
> }

     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 {
	    ...
	}

-- 
Eric.Sosman@[EMAIL PROTECTED]

 




 3 Posts in Topic:
static try method consolidation
thufir <hawat.thufir@[  2008-05-07 20:34:55 
Re: static try method consolidation
Eric Sosman <Eric.Sosm  2008-05-07 17:28:07 
Re: static try method consolidation
thufir <hawat.thufir@[  2008-05-07 21:29:52 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Wed Dec 3 14:47:18 CST 2008.