OCJP(Oracle Certified Java Program)

Totel:12 Click: 1 2 3
scjp1.5 Interview Questions And Answers

Page 1

 We will call it BadFoodException. 
  1. Create an enclosing class called MyException and a main() method, which will remain empty for now.
  2. Create a method called checkFood(). It takes a String argument and throws our exception if it doesn't like the food it was given. Otherwise, it tells us it likes the food. We can add any foods we aren't particularly fond of to the list.
  3. Now in the main() method, We will get the command-line argument out of the String array, and then pass that String on to the checkFood() method. Because it's a checked exception, the checkFood() method must declare it, and the main() method must handle it (using a try/catch). Do not have main() declare the method, because if main() ducks the exception,

Rethrowing is just we can throw the exception from a catch clause,we can also the same exception we just caught.

catch(IOException e) {
// Do things, then if you decide you can't handle it
throw e;

All catch clause the exception is thrown back to the calling method.id we want to be throw a checked exception from a catch clause, you must also declare that exception! In other words, you must handle and declare, as opposed to handle or declare.
The following example is illegal :public void doStuff() {
try {
// risky IO things
} catch(IOException ex) {
// can't handle it
throw ex; // Can't throw it unless you declare it

The Exception that a method can throw must be declare exception are subclass are RuntimeException.The throws keyword is used as follows to list the exceptions that a method can throw : 

void myFunction() throws MyException1, MyException2 {
// code for the method here

We can handle the the exception outsell and instance just declare it .and and either be caught there or continue on to be handled by a method further down the stack.

Each method must either handle all checked exceptions by supplying a catch clause or list each unhanded checked exception as a thrown exception. This rule is referred to as Java's handle or declare requirement.

Exception matching is the basically a type of hierarchy which is composed of the super class exception and the no of subtypes and we 'were interested in handling one of the subtypes in a special way but want to handle all the rest together, we need write only two catch clauses.

This process is called exception matching. Let's look at an example:

 import java.io.*;
public class ReadData {
 public static void main(String args[]) {
 try {
 RandomAccessFile raf =
 new RandomAccessFile("myfile.txt", "r");
 byte b[] = new byte[1000];
 raf.readFully(b, 0, 1000);
 catch(FileNotFoundException e) {
 System.err.println("File not found");
 catch(IOException e) {
 System.err.println("IO Error");

This short program attempts to open a file and to read some data from it. Opening and reading files can generate many exceptions, most of which are some type of IOException. Imagine that in this program we're interested in knowing only whether the exact exception is a FileNotFoundException. Otherwise, we don't care exactly what the problem is.

We can actually catch more than one type of exception in a single catch clause. If the Exception class that we specify in the catch clause has no subclasses, then only the specified class of exception will be caught. 

However, if the class specified in the catch clause does have subclasses, any exception object that subclasses the specified class will be caught as well.

We could write a catch clause like the following:

try {
// Some code here that can throw a boundary exception
catch (IndexOutOfBoundsException e) {

Goto Page:

1 2 3