module 4 -exception handling notes
module 4 -exception handling notes
Abstract class contains abstract and non- Interfaces do not contain non-abstract
abstract methods. methods but it contains only abstract
methods
Abstract methods contains instance variables Interfaces do not contain instance variables, it
and constants. contains only constants.
Abstract methods needs to be declared as All methods are implicitly public and abstract.
abstract explicitly.
Abstract class can extends another class, not An interface can extends another interfaces
interfaces only, not classes
Ex: Ex:
Exception Handling
1. The Java programming language uses exceptions to handle errors and other exceptional
events.
2. An exception is an abnormal condition that arises in a code sequence at run time.
3. In other words, an exception is a run-time error.
4. In Java, an exception is an object.
5. Exceptions can be generated by
- the Java run-time system, or
- they can be manually generated by our code.
1) Checked Exceptions
These are exceptional conditions that a well-written application should anticipate and
recover from.
Checked exceptions must follow the requirement - Catch or declare the exception
using throws.
Ex: java.io.FileNotFoundException.
All exceptions are checked exceptions, except for those indicated
by Error, RuntimeException, and their subclasses.
2) Unchecked Exceptions
Errors and runtime exceptions are collectively known as unchecked exceptions.
Unchecked exceptions need not follow the requirement - Catch or declare the
exception using throws.
Error:
These are exceptional conditions that are external to the application, and that the
application usually cannot anticipate or recover from.
Ex: An application successfully opens a file for input, but is unable to read the file because
of a hardware or system malfunction.
Runtime Exception:
These are exceptional conditions that are internal to the application, and that the
application usually cannot anticipate or recover from.
These usually indicate programming bugs, such as logic errors or improper use of an
API.
Ex: NullPointerException
Methods in Throwable
Throwable fillInStackTrace( ) Returns a Throwable object that contains a completed
stack trace. This object can be rethrown.
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at ExceptionTest1.doMoreStuff(ExceptionTest1.java:11)
CALL STACK
at ExceptionTest1.doStuff(ExceptionTest1.java:7)
at ExceptionTest1.main(ExceptionTest1.java:3)
class ExcExample{
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
finally
{
System.out.println("In finally block.");
}
Nested Try
A try statement can be inside the block of another try. This is called as Nested Try
statement.
int b = 42 / a;
System.out.println("a = " + a);
if(a==1)
a = a/(a-a); // division by zero
throw
It is possible for the program to throw an exception explicitly, using the throw statement.
The general form of throw is shown here:
// Demonstrate throw.
class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
System.out.println("Caught inside demoproc.");
}
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
System.out.println("Recaught: " + e);
}
}
}
throws clause
that callers of the method can guard themselves against that exception.
throws
type method-name(parameter-list) throws exception-list
{
// body of method
}
throw vs throws
throw throws
Java throw keyword is used to explicitly Java throws keyword is used to declare
throw an exception. an exception
Throw is used within the method. Throws is used with the method signature
You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws
IOException,SQLException
finally
1) finally creates a block of code that will be executed after a try /catch block has
completed and before the code following the try/catch block.
2) The finally block will execute whether or not an exception is thrown.
3) If an exception is thrown, the finally block will execute even if no catch statement
matches the exception.
// Demonstrate finally
class FinallyDemo {
// Throw an exception out of the method.
static void procA() {
try {
System.out.println("inside procA");
throw new RuntimeException("demo");
}
finally {
System.out.println("procA's finally");
}
}
// Return from within a try block.
static void procB() {
try {
System.out.println("inside procB");
return; Output:
}
finally { inside procA
System.out.println("procB's finally"); procA's finally
} Exception caught
} inside procB
// Execute a try block normally. procB's finally
static void procC() { inside procC
try { procC's finally
System.out.println("inside procC");
}
finally {
System.out.println("procC's finally");
}
}
public static void main(String args[]) {
try
{
procA();
}
catch (Exception e) {
System.out.println("Exception caught");
}
procB();
procC();
}
}
Chained Exceptions
The chained exception feature allows us to associate another exception with an
exception.
To allow chained exceptions, two constructors and two methods were added to
Throwable.
The constructors are shown here:
Throwable(Throwable causeExc)
Throwable(String msg, Throwable causeExc):
The chained exception methods supported by Throwable are:
getCause( ) and
initCause( ).
Output:
Caught:java.lang.ArithmeticException
Original clause:java.io.IOException: Original cause
1. try-with-resources
In Java, resources can be closed automatically by using try-with-resources statement
feature. This process is called as Automatic Resource management (ARM).
Syntax:
try(resource-specification)
{
}