RE: java question

  • From: "Sina Bahram" <sbahram@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sun, 21 Aug 2011 16:45:47 -0400

Just throw an exception. As in, throw your own, although you can probably find 
some good ones to just use in java.net or what not,
so no need to make your own exception, although you can if you wanted to.

Take care,
Sina

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Littlefield, Tyler
Sent: Sunday, August 21, 2011 4:27 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: java question

I have another question, I just ran into. I was making a server, so I 
figured I could set up the server to run accept in a second thread since 
the thread blocks. So accept throws a checked acception, IOException and 
I can't really return an error code, and you can't do something like 
throws IOException since that's not how the base class defines it. So, 
how would you return an error or something to let the application know 
that run didn't work? I guess you wouldn't technically return an error, 
but I need some sort of way to signal that there was a problem in 
accepting connections, anyway.
On 8/21/2011 10:06 AM, Sina Bahram wrote:
> Give class an accessibility identifier of public, just for good coding 
> practices.
>
> Return false in your catch because you have failed to make the connection, 
> and currently your code has a bug where it will fail to
> make the connection, then promptly output a message and return true, as if it 
> succeeded.
>
> Regarding your imports, it's extremely bad practice to import anything.*, so 
> instead explicitly import the things you need. Yes,
> this can sometimes create a long list, but for those cases, usually one is in 
> an IDE. Even if one isn't, it's better practice,
both
> for namespace pollution avoidance, as well as just explicit notes to the 
> reader, to not do .*.
>
> You are naming your methods with capital letters. This is best avoided for 
> best practice. Per the defacto community style
guidelines
> of java: Classes are initial caps camel case, packages are all lower case, 
> methods are initial lower case camel case, variables
are
> all lower. If you go back and change this, don't forget to correct your 
> string in your catch.
>
> Java has two kinds of exceptions: checked and unchecked. You only have to 
> catch checked exceptions. IOException is a checked
> exception, so yes you have to either catch it, or throw it up the chain. You 
> can throw it up the chain by simply saying throws
> IOException after the method declaration but before the opening brace
>
> Like this:
>
> Public Boolean listen(int port) throws IOException {
>
> I would not recommend doing that, and instead would do what you are doing, 
> which is handling that particular exception right then
> and there in listen, so no need to pass it up the chain.
>
> This brings us to your final question/problem, which you didn't explicitly 
> ask, but some might find confusing.
>
> You are in fact catching IOException, so why the heck is your java compiler 
> complaining that Listen isn't passing that IOException
> up the chain, and that's because of one of two things.
>
> Either A. you modified this program inadvertently in terms of ignoring 
> newlines or not pasting the whole code in, because the
error
> is really on line 15, not 19.
>
> Or B. your java compiler is super screwy.
>
> It's complaining that .close's IOException is not getting caught in finalize, 
> not the one thrown in listen, which you do catch
> successfully, so address it in finalize, and you're done.
>
> Oh, and I wouldn't split that string across two lines, if I were you. some 
> compilers are quite iffy about newline splitting of
> strings like that.
>
> Take care,
> Sina
>
> -----Original Message-----
> From: programmingblind-bounce@xxxxxxxxxxxxx 
> [mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Littlefield, Tyler
> Sent: Sunday, August 21, 2011 11:11 AM
> To: programmingblind@xxxxxxxxxxxxx
> Subject: java question
>
> Hello:
> I have the following code. When I try to compile, i get a really odd error.
> import java.net.*;
> import java.io.*;
>
> class Server
> {
>       private ServerSocket _listener;
>       public Server()
>       {
>           _listener = null;
>       }
>       protected void finalize()
>       {
>           if (_listener != null)
>           {
>               _listener.close();
>           }
>       }
>
>       public boolean Listen(int port)
>       {
>           if (port<  0 || port>  65535)
>           {
>               return false;
>           }
>
>           try
>           {
>               _listener = new ServerSocket(port);
>           } catch(IOException e) {
>               System.out.println("IOException caught in Server.Listen:
> "+e.getMessage());
>           }
>
>           return true;
>       }
> }
>
> First, I"m curious if I have done this right, or if there's a better way
> to set this up. Second, here's the error I get:
> Server.java:19: unreported exception java.io.IOException; must be caught
> or declared to be thrown
> so this means that I have to catch -all- exceptions? sort of makes some
> sense.
>


-- 

Take care,
Ty
my website:
http://tds-solutions.net
my blog:
http://tds-solutions.net/blog
skype: st8amnd127
My programs don't have bugs; they're randomly added features!

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: