Exception Handling


  • Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() methodA checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.

  • Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method. Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be, as they tend to be unrecoverable
  • In the given scenario
class First {

void test() throws Exception1, Exception2 { . . . }
}

class Second extends First {
void test() { . . . }
}

Q - Create a class called Third that extends Second and defines a test() method. What exceptions can Third’s test() method throw?

A - A method in a subclass cannot add new exception types that it might throw. Since it’s superclass, Second, does not define any exceptions in its test() method, Third can’t either.

Comments

Popular posts from this blog

jQgrid reload with new data

Introduction to jQgrid

Rich Client based UI technologies- A Comparison of Thick Client UI tools