Exception Handling

throwable is the  sub class of object class . and it have two sub class: a) Exception b) Error 

Error should not handle its system related which id not be able to recoverable like Assertion errors, virtual memory errors( outofmemoryerror, stackoverflowerror)

Exception can be handle at run time and a appropriate action can be taken. exception like Arithmetic , NullPointer, IOExecption etc.

Exception handling is managed by five keywords: try, catch, throw , throws and finally.

monitorerd code should be inside try block and try block should have {} if it have zero or more statements. followed by one or more catch statements or one finally statements or both but one of them is necessary after try block. At most one catch block executed irrespective catch blocks and program is continue to that try statement which thrown error during execution and then control goes to finally block if it is present in block.finally block is executed even error/exception handle or not.

Exception can be categorized in two types : a) Unchecked Exception: not necessary to check exception. error and its sub class and RunTimeException and its sub classes are unchecked exceptions.
b) Checked Exception: necessary to check other wise it will show compile time error.


Throws: if a method causing an exception that is not handle.


method_name throws Exception_list {
}

throw: built-in exception are thrown by JVM but user can also throw exception  
throw statement : throw e ;


finally : always executed 

if there are many catch statements are there after try block then then are executes in top-down sequence and catch stattement should be in sequence sub class to super class otherwise it will throw error like blow

catch ( RuntimeException e) {}
catch (NullPointerException e) {}
so it will not check NullPointerExecption because it always   be caught in RuntimeException .

so it should be in bleow sequence 

catch (NullPointerException e) {}
catch ( RuntimeException e) {}



Comments

Popular posts from this blog

Introduction to Java

Creational Design Pattern: Factory Design Pattern

Design Patterns: The Secret Weapon of Expert Developers