There are four main exception handling keyword which are commonely used, they are as follows:-
There are four main exceptation handling keyword which are commonely used, they are as follows:-
throw: Sometimes we explicitly want to create exception object and then throw it to interupt the normal processing of the program. throw keyword is used to throw exception to the runtime to handle it.
throws: When we
are throwing any checked exception in a method and not handling it,
then we need to use throws keyword in method signature to let caller
program know the exceptions that might be thrown by the method. The
caller method might handle these exceptions or propagate it to it’s
caller method using
throws keyword. We
can provide multiple exceptions in the throws clause and it can be
used with main() method also.
try-catch: We use try-catch block for exception handling in our code. try is the start of the block and catch is at the end of try block to handle the exceptions. We can have multiple catch blocks with a try and try-catch block can be nested also. catch block requires a parameter that should be of type Exception.
finally: finally block is optional and can be used only with try-catch block. Since exception halts the process of execution, we might have some resources open that will not get closed, so we can use finally block. finally block gets executed always, whether exception occurrs or not.
Read More →
Core Java Interview Questions
Object Oriented Programming(OOP) Questions and Answers
Collections Interview Questions
Java Exceptions Interview Questions
Java Threads Interview Questions
Collection framework in java interview questions
oops concepts with example in java
Java Serialization Interview Questions
Top 20 Core Java Interview Questions with Answers
String Interview Question in java With Example
synchronization interview questions
Java Reflection Interview question
Java Executor Framework (JDK 1.5) Interview Questions
JDK 1.7 interview Questations and Answers
Java I/O interview question with example