Hi all, Has anyone else noticed some discrepancies in Sun's documenation of exceptions? For example, I'm using the Socket constructor which takes two arguments: (1) a String which is the host and (2) and int which is the port number. The declaration of this method in the documentation is public Socket(String host, int port) throws UnknownHostException, IOException However, if you read down a few lines, under the Throws header it says the method throws IOException and SecurityException. Must I catch all three? If so, why aren't all three included in the throws part of the declaration? I am a little confused. Thanks in advance. Stephanie
Stephanie, No... this is not an error in documentation... SecurityException is a RunTimeException, thus it is an unchecked exception and does not have to be declared under the throws clause of the method, or caught in a try/catch block. In the JLS this is mentioned in section 11.2 :
"The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See �11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine."
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Joined: Jun 14, 2000
Nathan, Thanks for your reply. I should have looked at the base class for SecurityException before posting. Just being lazy I guess. Thanks again. Stephanie
Stephanie, Look at it this way, you were REALLY doing a favor for the rest of us!!! See, I got a review on unchecked exceptions AND learned a little more about SecurityExceptions. So this was really more in the line of Altruism (NEVER laziness).
"JavaRanch, where the deer and the Certified play" - David O'Meara