04 Month
01 Hours
Checked Exception | Unchecked Exception |
---|---|
Checked Exception directly inherit the property of throwable class |
Unchecked Exception inherit the Runtime Exception |
Occurs at Compile time | Occurs generally on run time |
throws can handle checked exception |
throws can handle both |
Sql exception , io exception and class notfound exception are part of checked exception |
arrayIndex OuterbondException, arithmetic exception and null pointer exception are example of it. |
Error | Exception |
---|---|
Error occurs due to lack of system resource |
Exception occurs due to code in program |
Error generally occurs on runtime |
Exception generally occurs on compile and runtime |
Once the error occurred program will terminated |
Program will not terminated if we handle it will follow |
|
|
ClassNotFound Exception | NoClassDefine Error |
---|---|
ClassNotFound exception occurred when path of exception is not executed correctly |
NoClassDefine error occurred when class definition was present at compile time but missed at run time. |
Try: try block is used to defined the exception ,it is definition block which must include with catch and finally .only try block will not executed ,either catch or finally block must be present in the code along with try block.
Catch: catch block is used to handled the exception .it must be included with try block and followed by finally block. for handling multiple exceptions, we may have multiple catch block.
Try without catch block is possible but catch without try is not possible.
When multiple catch block is used then unchecked exception will be handled first and then exception class other wise unreachable catch lock exception occurred.
Finally: finally block is structure of block which execute to check whether exception is handled properly or not.it is independent of the execution of try and catch block. Try without catch but with finally will be possible.
Final | Finally | Finalize |
---|---|---|
Final is a keyword | Finally is block | Finalize is a method |
Final restricts the
access of class, method and variable |
Finally will execute in
exception handling to check whether exception is handled properly or not |
Finalize is used for garbage collection |
Final when used with class that cannot be extended further | Finally will not executed when system crashed or system. exit() method called | It will execute after system.gc() method called. |
Final when used with method, that cannot be overridden | ||
When used with variable that cannot be reinitialized. |
Throw() | Throws() |
---|---|
Throw is followed by instance | Throws is followed by class |
Throw is not used to handle checked exception | Throws is used to handle checked exception |
Throw is used to call exception explicitly | Throws defined the exception |
Throw is present inside method | Throws are present in method signature |
Throw handle only one exception at a time | Throws can handle more than one exception at a same time |
When we extend exception class then custom checked exception is created whereas when runtime exception class is extended hen custom unchecked exception is created.
import java.util.Scanner; class IlligalException extends Exception { public IlligalException(String s) { super(s); } } public class CustomException { public static void validate(int age) throws IlligalException { if (age < 20) { throw new IlligalException("Age is below 19"); } else { throw new IlligalException("please ignore"); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the age"); int age = sc.nextInt(); try { validate(age); } catch (Exception e) { System.out.println(e); } } }
When exception I thrown by main () method it will terminate the program at runtime and print exception method at console. If file not founded filenotfoundexception thrown.
All multi-threading level questions such as thread ,process,synchronization.
Visit