0

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); }
}
Mat
  • 202,337
  • 40
  • 393
  • 406

7 Answers7

4

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Henri
  • 5,065
  • 23
  • 24
  • 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 –  Sep 14 '09 at 21:30
3

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
  • 76,767
  • 18
  • 98
  • 146
2

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

DJ.
  • 6,664
  • 1
  • 33
  • 48
2

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
  • 38,619
  • 8
  • 86
  • 96
  • 1
    Are you sure unchecked exceptions don't need to be caught? – Tom Hawtin - tackline Sep 14 '09 at 22:20
  • 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 Sep 14 '09 at 22:39
  • 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 : ). – João Silva Sep 15 '09 at 09:08
0

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.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 2
    -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 Sep 14 '09 at 22:14
  • 1
    So I should program everything in notepad if I am starting to learn a new programming language? – SeanJA Sep 14 '09 at 22:31
  • 2
    "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". – Decker Sep 14 '09 at 22:52
  • 2
    "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. – Denis Tulskiy Sep 15 '09 at 00:19
  • 1
    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 Sep 15 '09 at 00:36
  • 1
    @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*. – João Silva Sep 15 '09 at 08:56
  • @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 Sep 15 '09 at 10:50
  • 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 Sep 15 '09 at 10:53
  • 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 Sep 15 '09 at 11:03
0

To make the code compile, the line should read:

catch (StackOverflowError exc) {x++;}
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

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
  • 17,140
  • 7
  • 32
  • 33