Topics

6/recent/ticker-posts

Exceptions in Java

 Exceptions & Exception Handling



Introduction to Exception : Exceptions topic deals with 5 clauses and Exception classes hierarchy:

The Five  clauses are:

i) throw - raises exception

ii) throws - reports exception

iii) try - monitors exception

iv) catch - handles exception

v) finally - finally can be last block in the method that executes both in normal and abnormally termination of the method exception.

 after finally block in the method we can write statements but try catch blocks should not be written after finally. In finally we can write try/catch blocks more. 

Usually in the final block we will destroy all the local objects. try followed we can write any number of catch blocks and finally block or we can directly write finally block.


Error: 

Basically Errors are classified in to two types they are 

  • compile Type errors 
  • Run Time Errors 

 Compile time errors:

  •  Causes because wrong syntax used, unavailable resources such as variable/field, method, class etc. Compilation error must be rectified and recompiled again.

 Runtime errors:

  •  Error which cause at runtime because of memory problem or unavailable resource or version error or linking error. Some of the errors can be handled using try/catch block, some cannot be handled. Runtime Error classes derives from Error super class. Error class super class is Throwable. Throwable super class is Object.

NoSuchFieldError

NoSuchClassError

NoSuchMethodError

OutOfMemory

BufferOverflow

ClassVersionError

NoClassDefFoundError

UnsatisfiedLinkError

ClassCircularityError

ClassFormatError


Exception:

 Exceptions always raises at runtime. Abnormal condition araised in the program is an Exception.

 Exception super class is Throwable.

 Classes which extends Exception class are called as CheckedExceptions.

 Checked Exception:

  •  Checked Exception are exceptions which causes because system related errors such as filenotfound, socket connection problem, DB connection error, SQL execution failure etc.
  •  When a method/constructor reports CheckedException compiler forces us to handle such Exceptions in the method.

 

 Given in API/in rt.jar file:

 public class FileInputStream extends InputStream {

public FileInputStream(String fileName) throws FileNotFoundException {

if(fileName not exist)

throw new FileNotFoundException(fileName+" requested file is not found");

}

public int read() throws IOException {

if(file reached to end-of-the-file)

throw new EOFException();

}

 }

 In the developer written client program:

 class FISClient {

public static void main(String rags[]) {

try {

FileInputStream fis=new FileInputStream("studentDetails.txt");

int i=fis.read();

  fis.read();

  fis.read();

}

catch(FileNotFoundException fnfe) {

// print the exception

fnfe.printStackTrace();

// or

System.err.println(fnfe);

// terminate the program

// System.exit(0);

}

catch(IOException ioe) {

}

catch(Exception ioe) {

}

catch(Throwable t) {

}

try {

// int i=sc.nextInt();

int i=Integer.parseInt(rags[0]);

// int j=sc.nextInt();

int j=Integer.parseInt(rags[1]);

int k=i/j;

}

catch(ArithmeticException ae) {

}

try {

int iarr[]={1,2};

System.out.println(iarr[2]); 

}

catch(ArrayIndexOutOfBoundsException aioobe) {

}

}

 }

For More Information please contact Instructor: 9848111288(Surya Narayana ActiveNet)

Post a Comment

0 Comments