• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading about exception in Java API

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, can anybody tell me the difference between the
two types of "throws" in the Java 5 API text:

In the File class:
-------------------
length

public long length()

Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.

Returns:
The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist

Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file
------------------
createNewFile

public boolean createNewFile() throws IOException
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading through that document myself, it seems that there is only one type of "throw" being discussed, however, the methods listed throw diffrent exceptions depending on the condition that causes them;

for example: If an IO error occurs, IOException is thrown. If the security manager denies write access to a file, a SecurityException is thrown.

Excuse me if im missing the point of your question completly.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be you are just asking about the documentation conventions. The first "Throws" you bolded is part of the javadoc. That is, at the top of each method, there is comment block that explains, in plain language, what the method does. A special "tag", either @throws or @exception, is used to describe any exceptions thrown by the method. The Java compiler ignores comments, but another program (javadoc) understands these tags and automatically formats documentation in the form you are reading.

The second "throws" is actually part of the Java code. It tells the compiler what to expect can go wrong when the method is run. For checked exceptions (which is many of them), if a problem can occur in the method, it must either be handled inside the method, or declared in the method's "throws".

For well-documented code, there should be descriptions under the first "Throws" of every exception listed after the second "throws".
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For well-documented code I would expect descriptions for unchecked exceptions as well. For example, "Throws NullPointerException if arg1 is null."
 
C Law
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the feedback.

The difference that confused me is:

If I put the method with the second (type of) bold throw, like this:



The compiler will complain a checked exception (in this case IOException)
is not caught or declared. This is an error and the code will not compile.

However, for methods with the first type of bold throw, it's fine not
to declare/handle it, as in the following example:




So I was trying to see which "throw" means the method it is associated
with MUST declare or handle exception (e.g. is it safe to say for
all methods in the API doc that comes with the second type of throw,
like createNewFile(), we must put it in try...catch, or declare it.

Thanks.
[ March 22, 2006: Message edited by: C Law ]
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, sort of. The File.length() method only throws a SecurityException, which is unchecked. You're right, it is described in the Javadoc, but not declared in the Java code. Because it's unchecked, it doesn't have to be declared, but it could be. The only way to know for sure it's an unchecked exception is to look at it in the documentation and see that it descends from RuntimeException. An IOException is a checked exception, so File.createNewFile() must declare throwing it. There is no obligation for a coder to document either checked or unchecked exceptions, but I believe any exception that is declared in the code for the method (whether checked or unchecked) will be automatically inserted into the "Throws:" section by Javadoc, even if there is nothing to say about it.

The bottom line is: what's listed after "throws" is part of the Java code. What is listed after "Throws: " is just documentation.
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic