What is an exception?
An abnormal condition that arises in a code sequence at run time is called an exception. During the execution of a program an exception event occurs that disturbs the program flow. An object of an exception subtype is instantiated when an exception is thrown and handed it to the exceptional handler as an argument to the catch clause.
An actual catch clause, as shown below:
[code lang=”java”]try {
// some code here
}
catch (ArrayIndexOutOfBoundException e) {
e.printStackTrace();
}
[/code]
In this example, e is an instance of the ArrayIndexOutOfBoundExceptionclass. As with any other object, we can call its methods.
What are the reasons that an exception can occur?
An exception can occur due to many different reasons like hardware failures, resource exhaustion and old bugs.
What is an exceptional handler?
The code which is responsible for doing something about the exception is called exceptional handler and it catches the thrown exception.
Name the five keywords that are used in Exception handling?
There are five keywords that are used in Exception handling:
1.try
2.catch
3.finally
4.throw
5.throws
Explain exceptional handling in java?
When an exception occurs the execution of the program is transferred to an appropriate exception handler and for this purpose try-catch-finally block is used to handle the exception.The code in which the exception occur is enclosed within in a try block, called as a guarded region.
The catch clause matches a specific exception to a block of code which handles that exception. By the java run-time system, system-generated exceptions are automatically thrown. Use the keyword throw to manually throw an exception. By a throws clause any exception that is thrown out of a method must be specified. The clean up code that needs to be executed is put inside the finally block no matter the exception occurs or not.
When exception is thrown by main method what happens?
Java runtime system terminates when exception is thrown by main method.
Does we have the try block without catch or finally block?
No, try block must have a catch or finally block associated with it.
In which java package exceptions are defined ?
Exceptions are defined in Java.lang.Exception package.
Explain Java Exception Hierarchy ?
The top level class in exception hierarchy is Throwable, from Throwable Error and Exception extends . Errors are the conditions that can’t be recovered so they are not handled by a java program. Exception is further extended by Runtime Exception.
What is returned to the calling method if both catch and finally return a value?
If both blocks i.e. catch and finally define return statements, the finally block will give a value to the calling method.
For example:
[code lang=”java”]
class ReturnValue {
int getInt() {
try {
String[] students = {"saanvi", "Lola"};
System.out.println(students[5]);
}
catch (Exception e) {
return 20;
}
finally {
return 30;
}
}
public static void main(String args[])
{
ReturnValue var = new ReturnValue();
System.out.println(var.getInt());
}
}
[/code]
The output is
30
Can we declare my methods to throw a checked exception,
instead of handling it?
If a method doesn’t handle the checked exceptions thrown by a method it calls, using the throws clause it can throw these exceptions in its own method signature.
For example
[code lang=”java”]
import java.io.*;
public class ReThrowException {
public void myMethod() throws IOException {
FileInputStream fism= new FileInputStream("javavapor.txt");
fism.close();
}
}
[/code]
Any type of method that calls myMethod must either catch the exception IOException or declare that it will be rethrown in its method signature.
What is the meaning of ‘Ducking’ the exception ?
‘Ducking’ the exception means that if a method does not handle the exception but simply declares it using throws clause.
Is the order of the exceptions caught in the catch blocks matter ?
Yes, the order is matter for related classes sharing an IS-A relationship. While the order doesn’t matter for unrelated classes.
Now to catch an exception of the base class before an exception
of the derived class, the code not compile. Following given code shows that:
[code lang=”java”]
import java.io.*;
public class Exception {
public static void main(String args[])
{
FileInputStream fsm= null;
try {
fsm = new FileInputStream("javavapor.txt");
fsm.close();
}
catch ( IOException io) {
System.out.println("IOException");
}
catch ( FileNotFoundException fnf) {
System.out.println("file not found");
}
}
}
[/code]
In the above case the exception handler for the derived class can
never be reached, so the code will not compile. The java compiler decline to compile the code because the exception handler code never execute later.
Always remember that the order matters. A thrown exception view for an exception handler, starting with the first handler and working
toward the last. Java doesn’t compile code if it contains unreachable statements.
This is very important notes and easy to understand..thank you ..plz add whole core java notes..
Yes sure.