views:

132

answers:

4

Do I have to take a look at the java api everytime I instantiate an object / call a method from a class in it? Also, do I always have to know which classes are in the java api and which are not?

+1  A: 

I use Eclipse IDE which will make sure you account for all thrown exceptions. I highly recommend it.

http://www.eclipse.org

mcassano
Most IDEs do this, not just Eclipse.
Thomas Owens
Correct, good point, I was indicating Eclipse as my personal recommendation.
mcassano
+8  A: 

If the exception is checked, then the java compiler will force your invoking method to either catch the exception or declare that it could throw the exception.

If the exception that is thrown inherits from Error or RuntimeException - I.e. is unchecked, then you have no way of knowing besides javadoc and looking at the code.

A good example of the latter, is NumberFormatException, thrown by Double.parseDouble(String). The only way to know is that the javadoc tells you it could throw this exception.

Modern IDEs (Eclipse, Netbeans, IntelliJ etc) provide easy access to this documentation.

daveb
+1  A: 

Knowing what exceptions a function throw is not different from knowing what arguments it needs, and what type it returns.

You either know it, look it up, or use an IDE that does this for you. BTW for checked exceptions you will get a compile-time error, so that can also be an option.

Zed
+1  A: 

For you second question, In general...

Packages that start with "java." or "javax." are in the J2SE API.

Most packages starting with a internet domain prefix like "com." or "org." are supplied by third parties. Don't count on com.sun being stable though.

Packages with none of prefixes the above are likely not following the package naming guidelines or predate them.

Chris Nava
You're thinking of com.sun packages - those *aren't* guaranteed to be stable. The javax package is as stable as java - it's differentiated that way to let you know it's a "newer" library that came out with Java 2 (i.e. Java 1.2 as opposed to Java 1 or 1.1) - however that was the only time they've done that - lots of stuff was added in Java 1.5, but they didn't add a javaxx namespace.
Nate
You are correct. edited.
Chris Nava