views:

179

answers:

7

I have a problem at school. I'm new to programing and I need some help. Please complete the dotted line:

import java.net.*; 
import java.io.*
public class Test{
public static void main (String arg[]} 
    int x=0;
    try { 
        ServerSocket s=new ServerSocket ( k ); 
        s.close();
    } 
    catch (..........................) {x++;}
    catch(IOException e) {}
    }
    System.out.println( "Total "+ x); }
}
+4  A: 

Look up in the documentation what exceptions the constructor of ServerSocket can throw and what exception the close function of ServerSocket can throw. One of them is probably IOException, just look up what else.

Henri
IOException - if the bind operation fails, or if the socket is already bound. SecurityException - if a SecurityManager is present and its checkListen method doesn't allow the operation. IllegalArgumentException - if endpoint is a SocketAddress subclass not supported by this socket So witch one :D
+2  A: 

The answer can be found at the JavaDoc for ServerSocket constructor that you use.

DJ
+2  A: 

You need to catch (or declare that your method throws) every Checked Exception that is declared by the code that you are calling. These are documented in the JavaDoc and on the method signature. Unchecked exceptions, like IllegalArguementException, do not need to be caught.

akf
Are you sure unchecked exceptions don't need to be caught?
Tom Hawtin - tackline
Good point. I guess it depends on how you define need. I was referring to compile-time checks, but there could be cases where 'it would be good' to catch RuntimeExceptions - ie cases where you can recover gracefully. http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html points to RuntimeExceptions as "problems that are the result of a programming problem" - however, IMO that is written towards Exception implementors, not those catching them.
akf
They need to be caught, when it's appropriate. There's not a single rule that dictates that. For instance, `WebServiceException` is a very common unchecked exception that is usually caught by most applications, but there are many others. Another one is the exception that is missing at the OP : ).
JG
A: 

If you use an IDE, such as Eclipse, you can easily fix those problems and many others, as the IDE will point out exactly what Exceptions need to be caught, saving you from a trip to the Javadoc page which, by the way, is easily accessed directly from the IDE.

@akf: That's not the opinion of the instructors of some major CS schools. For example, the "Introduction to Computer Science | Programming Methodology" course of Stanford uses a modified version of Eclipse. I think it's a much better way to learn a language, avoiding those situations where you are fixing a problem, compile, and then fix the new problem. This can be frustrating to newcomers. An IDE is just a tool to aid programming, it doesn't cause dependence, and it certainly won't stop you from coding without it.

I do agree, however, that it's important to know the basics, as far as using java or javac is concerned, just in case you ever find yourself with a terminal in front of you.

JG
-1: it is better for a new programmer not to rely on IDEs too much. It is better to learn the basics manually, then graduate to the IDE to make life easier.
akf
So I should program everything in notepad if I am starting to learn a new programming language?
SeanJA
"So I should program everything in notepad if I am starting to learn a new programming language?" - Well, everyone's different but I've actually found this to be really educational. I usually use something with syntax highlighting like vim, (or a guy could use emacs, textpad, notepad++, etc). But I've found that just doing things from the ground up without shortcuts at first can lead to a greater understanding of what's going on. Then later, when using fancy tools, the stuff they do behind the scenes isn't "magic".
JPDecker
"It is better to learn the basics manually" - Let me disagree with you. Basics can be really confusing and an IDE helps a lot in cases like this one. And automplete drastically speeds up learning curve, because when you start to use some class, you can see all its functionality with inline documentation lookup as oppose to using methods from the book.
tulskiy
I agree completely with Pilgrim, were he/she using an ide, you would probably not see import java.io.* but rather the exact class that they were going to be using as well, teaching them where functionality is rather than just 'load all of package and in there is somewhere, hidden to me is something that I need'.
SeanJA
@akf: What's with all that hate? I just argued that it'll make the task a lot easier, I don't think you agree that using, say, pico, is more productive than using an IDE. For beginners especially, I think the autocomplete feature and telling you why your syntax is not correct is a very good way to learn. But hey, if you say "It's better to do X." with such an authoritative manner, who are the Stanford guys to doubt from that *magna sapientia*.
JG
@JG: I didnt mean to trigger this sort of reaction. The operative concept in my original comment was 'rely on IDEs too much' which is what I read into your original answer, (paraphrased for emphasis) 'why do all this thinking? do yourself a favor and let the IDE do it for you." That said, I like your edit.
akf
Also, with the sympathetic votes you've received, a simple comment about using an IDE, coupled with a simple statement like mine has garnered you 20+ points. Not bad for evening's work.
akf
I was thinking that this would be a good topic for a CW post, but it seems that someone has already posted this question (http://stackoverflow.com/questions/1109678/when-should-you-start-using-an-ide-when-learning-a-language).
akf
+3  A: 

A hint: if you use a modern IDE, it'll tell you. For example, just write without the try and catch blocks

import java.net.*; import java.io.*
public class Test{
public static void main (String arg[]} 
    int x=0;
    ServerSocket s=new ServerSocket ( k ); 
    s.close();
    System.out.println( "Total "+ x); }
}

The IDE will underline the code and give you a suggestion, click on it and it'll insert the appropriate Exceptions automatically.

JRL
Or javac will do. It won catch unchecked exceptions and exceptions you have caught a base class of.
Tom Hawtin - tackline
A: 

To make the code compile, the line should read:

catch (StackOverflowError exc) {x++;}
Tom Hawtin - tackline
Are you kidding?
Tim Büthe
A: 

As you mention in your comment on Henri, you have to choose between either SecurityException or IllegalArgumentException.

The significance here lies in the variable x. What is x supposed to count? My guess would be the former, but there should be a hint in your assignment.

(BTW: I only see a mention of SecurityException besides IOException in the documentation)

NomeN