Friday, 19 September 2008

Java 2 Certification By Phillip Heller, Simon Roberts - Part3

Couple Of Days back i started reading Complete Java 2 Certification Study Guide, 5th Edition By Phillip Heller, Simon Roberts.

I wanted to share few important quotations i found from the next 2 chapters(5-6) of the book.

1) Catching Exceptions

One way to call a method that throws an exception is to create a try block and a catch block.
The structure looks like this:


try {
// Exception-throwing code
}
catch (Exception_type name) {
// Exception-handling code
}



The try block contains code that throws exceptions. Code that doesn’t throw exceptions may also be included.

2) Declaring Exceptions
There is a way to call exception-throwing methods without enclosing the calls in try blocks. A method declaration may end with the throws keyword, followed by an exception type, or by multiple exception types followed by commas. A throws declaration may be combined without restriction with any other modifiers.

Here are some examples:


public void throwsOne() throws IOException {

}
private static synchronized int throwsTwo()
throws IOException, AWTException {

}



When a method declares that it throws exceptions, any method call that throws exception types listed in the method declaration does not need to appear in a try block. So in the examples above, throwsOne() may contain calls to methods that throw IOException, without enclosing those calls in try blocks. Similarly, within throwsTwo(), any calls to methods that throw IOException or AWTException need not appear in try blocks.Of course, methods that call throwsOne() or throwsTwo() must either enclose those calls in try blocks or declare that they, too, throw the exception types.

3) Using Assertions

Assertions are commonly used to check preconditions, postconditions, and class invariants.

A precondition is a constraint that must be met on entry of a method. If a method’s preconditions are not met, the method should terminate at once before it can do any damage. A method’s preconditions are typically functions of its arguments and the state of its object. Argument range checking at the start of a method is a common form of precondition testing.

A postcondition is a constraint that must be met on return from a method. If a method’s postconditions are not met, the method should not be allowed to return. A method’s postconditions are typically functions of its return value and the state of its object. In a general sense, if a precondition fails, the problem lies in the method’s caller, whereas if a postcondition fails, the problem lies in the method itself.

A class invariant is a constraint on a class’s state that must be met before and after execution of any non-private method of a class. (Private methods might be used to restore the required state after execution of a non-private method.)

About the Authors

Philip Heller is a technical author, novelist, public speaker, and consultant. He has been instrumental in the creation and maintenance of the Java Programmer and Developer exams. His popular seminars on certification have been delivered internationally. He is also the author of Ground-Up Java (available from Sybex), which uses interactive animated illustrations to present fundamental concepts of Java programming to new programmers.

Simon Roberts worked for Sun Microsystems for nine years as an instructor, an authority on the Java language, and the key player in the development of the entire Java certification program. He is now a consultant and instructor, specializing in Java and security. He is also a flight instructor.

No comments: