Unit – III
Exception Handling and I/O
Exceptions-exception hierarchy-throwing and catching exceptions-built-in
exceptions, creating own exceptions, Stack Trace Elements. Input /Output
Basics-Streams-Byte streams and character streams-Reading and Writing
Console-Reading and Writing Files Templates
Difference between error and exception
Errors indicate serious problems and abnormal conditions that most
applicationsshould not try to handle. Error defines problems that are not
expected to be caught under normal circumstances by our program. For
example memory error, hardware error, JVM error etc.
Exceptions are conditions within the code. A developer can handle such
conditionsand take necessary corrective actions. Few examples
DivideByZero exception
NullPointerException
ArithmeticException
ArrayIndexOutOfBoundsException
o An exception (or exceptional event) is a problem that arises during the
execution of a program.
o When an Exception occurs the normal flow of the program is disrupted
and the program/Application terminates abnormally, which is not
recommended, therefore, these exceptions are to be handled.
o If an exception is raised, which has not been handled by programmer
then program execution can get terminated and system prints a non user
friendly error message.
Ex: Exception in thread "main"
[Link]: / by zero at
[Link]([Link])
Where, ExceptionDemo : The class name
main : The method name
[Link] : The filename
java:5 : Line number
An exception can occur for many different reasons. Following are some
scenarios where an exception occurs.
A user has entered an invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications or
the JVM hasrun out of memory.
Exception Hierarchy
All exception classes are subtypes of the [Link] class. The
exception class is a subclass of the Throwable class.
Key words used in Exception handling
There are 5 keywords used in java exception handling.
A try/catch block is placed around the code that might generate an
1. try exception. Code within a try/catch block is referred to as protected code.
A catch statement involves declaring the type of exception we are trying
2. catch to catch.
A finally block of code always executes, irrespective of occurrence of an
3. finally Exception.
It is used to execute important code such as closing connection, stream
4. throw etc. throw is used to invoke an exception explicitly.
5. throws throws is used to postpone the handling of a checked exception.
Syntax : //Example-predefined Excetion - for
try //ArrayindexoutofBounds Exception
{ public class ExcepTest
//Protected code {
} public static void main(String args[])
{ int a[] = new int[2];
catch(ExceptionType1 e1) try
{ [Link]("Access element three :" +
{ a[3]);
//Catch block }
} catch(ArrayIndexOutOfBoundsException e)
catch(ExceptionType2 e2) { [Link]("Exception thrown :" + e);
{ }
//Catch block finally
} { a[0] = 6;
catch(ExceptionType3 e3) [Link]("First element value: " + a[0]);
[Link]("The finally statement is
{ executed");
//Catch block }
} }
finally }
{ Output
Exception thrown
//The finally block always :[Link]
executes. First element value: 6
} The finally statement is executed
Note : here array size is 2 but we are trying to access
3rdelement.
Uncaught Exceptions
This small program includes an expression that intentionally causes a divide-by-
zero error: class Exc0 { public static void main(String args[]) { int d = 0; int a =
42 / d; } } When the Java run-time system detects the attempt to divide by zero,
it constructs a new exception object and then throws this exception. This causes
the execution of Exc0 to stop, because once an exception has been thrown, it
must be caught by an exception handler and dealt with immediately
Any exception that is not caught by your program will ultimately be processed
by the default handler. The default handler displays a string describing the
exception, prints a stack trace from the point at which the exception occurred,
and terminates the program. Here is the exception generated when this example
is executed:
[Link]: / by zero at [Link]([Link])
Stack Trace:
Stack Trace is a list of method calls from the point when the application was
started to the point where the exception was thrown. The most recent method
calls are at the top. A stacktrace is a very helpful debugging tool. It is a list of
the method calls that the application was in the middle of when an Exception
was thrown. This is very useful because it doesn't only show you where
the error happened, but also how the program ended up in that place of the
code.
Using try and Catch
To guard against and handle a run-time error, simply enclose the code that you
want to monitor inside a try block. Immediately following the try block, include
a catch clause that specifies the exception type that you wish to catch. A try and
its catch statement form a unit. The the following program includes a try block
and a catch clause that processes the ArithmeticException generated by the
division-by-zero error:
class Exc2 {
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
[Link]("This will not be printed.");
} catch (ArithmeticException e) { // catch divide-by-zero error
[Link]("Division by zero.");
}
[Link]("After catch statement.");
}
}
This program generates the following output:
Division by zero.
After catch statement.
The call to println( ) inside the try block is never executed. Once an exception is
thrown, program control transfers out of the try block into the catch block.
Multiple catch Clauses
In some cases, more than one exception could be raised by a single piece of
code. To handle this type of situation, you can specify two or more catch
clauses, each catching a different type of exception. When an exception is
thrown, each catch statement is inspected in order, and the first one whose type
matches that of the exception is executed. After one catch statement executes,
the others are bypassed, and execution continues after the try/catch block.
The following example traps two different exception types:
// Demonstrate multiple catch statements.
class MultiCatch {
public static void main(String args[]) {
try {
int a = [Link];
[Link]("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
} catch(ArithmeticException e) {
[Link]("Divide by 0: " + e);
} catch(ArrayIndexOutOfBoundsException e) {
[Link]("Array index oob: " + e);
}
[Link]("After try/catch blocks.");
}
}
Here is the output generated by running it both ways:
C:\>java MultiCatch
a=0
Divide by 0: [Link]: / by zero
After try/catch blocks.
C:\>java MultiCatch TestArg
a=1
Array index oob: [Link]
After try/catch blocks.
Nested try Statements
The try statement can be nested. That is, a try statement can be inside the block
of another try. Each time a try statement is entered, the context of that exception
is pushed on the stack. If an inner try statement does not have a catch handler
for a particular exception, the stack is unwound and the next try statement’s
catch handlers are inspected for a match. This continues until one of the catch
statements succeeds, or until all of the nested try statements are exhausted. If no
catch statement matches, then the Java run-time system will handle the
exception.
// An example of nested try statements.
class NestTry {
public static void main(String args[]) {
try {
int a = [Link];
/* If no command-line args are present,
the following statement will generate
a divide-by-zero exception. */
int b = 42 / a;
[Link]("a = " + a);
try { // nested try block
/* If one command-line arg is used,
then a divide-by-zero exception
will be generated by the following code. */
if(a==1) a = a/(a-a); // division by zero
/* If two command-line args are used,
then generate an out-of-bounds exception. */
if(a==2) {
int c[] = { 1 };
c[42] = 99; // generate an out-of-bounds exception
}
} catch(ArrayIndexOutOfBoundsException e) {
[Link]("Array index out-of-bounds: " + e);
}
} catch(ArithmeticException e) {
[Link]("Divide by 0: " + e);
}
}
}
C:\>java NestTry
Divide by 0: [Link]: / by zero
C:\>java NestTry One
a=1
Divide by 0: [Link]: / by zero
C:\>java NestTry One Two
a=2
Array index out-of-bounds:
[Link]
throw
it is possible for your program to throw an exception explicitly, using the throw
statement. The general form of throw is shown here:
throw ThrowableInstance;
Here, ThrowableInstance must be an object of type Throwable or a subclass of
Throwable.
Primitive types, such as int or char, as well as non-Throwable classes, such as
String and
Object, cannot be used as exceptions. There are two ways you can obtain a
Throwable object: using a parameter in a catch clause, or creating one with the
new operator.
The flow of execution stops immediately after the throw statement; any
subsequent statements are not executed. The nearest enclosing try block is
inspected to see if it has a catch statement that matches the type of exception. If
it does find a match, control is transferred to that statement. If not, then the next
enclosing try statement is inspected, and so on. If no matching catch is found,
then the default exception handler halts the program and prints the stack trace
// Demonstrate throw.
class ThrowDemo {
static void demoproc() {
try {
throw new NullPointerException("demo");
} catch(NullPointerException e) {
[Link]("Caught inside demoproc.");
throw e; // rethrow the exception
}
}
public static void main(String args[]) {
try {
demoproc();
} catch(NullPointerException e) {
[Link]("Recaught: " + e);
}
}
}
Here is the resulting output:
Caught inside demoproc.
Recaught: [Link]: demo
Throws
If a method is capable of causing an exception that it does not handle, it must
specify this behaviour so that callers of the method can guard themselves
against that exception. You do this by including a throws clause in the method’s
declaration. A throws clause lists the types of exceptions that a method might
throw. This is necessary for all exceptions, except those of type Error or
RuntimeException, or any of their subclasses. All other exceptions that a
method can throw must be declared in the throws clause. This is the general
form of a method declaration that includes a throws clause:
type method-name(parameter-list) throws exception-list
{
// body of method
}
Here, exception-list is a comma-separated list of the exceptions that a method
can throw.
class ThrowsDemo {
static void throwOne() throws IllegalAccessException {
[Link]("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwOne();
} catch (IllegalAccessException e) {
[Link]("Caught " + e);
}
}
}
Here is the output generated by running this example program:
inside throwOne
caught [Link]: demo
finally
The finally keyword is designed to address this contingency. 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. The finally block will execute
whether or not an exception is thrown. If an exception is thrown, the finally
block will execute even if no catch statement matches the exception. Any time a
method is about to return to the caller from inside a try/catch block, via an
uncaught exception or an explicit return statement, the finally clause is also
executed just before the method returns. This can be useful for closing file
handles and freeing up any other resources that might have been allocated at the
beginning of a method with the intent of disposing of them before returning.
The finally clause is optional.
// Demonstrate finally.
class FinallyDemo {
// Through an exception out of the method.
static void procA() {
try {
[Link]("inside procA");
throw new RuntimeException("demo");
} finally {
[Link]("procA's finally");
}
}
// Return from within a try block.
static void procB() {
try {
[Link]("inside procB");
return;
} finally {
[Link]("procB's finally");
}
}
// Execute a try block normally.
static void procC() {
try {
[Link]("inside procC");
} finally {
[Link]("procC's finally");
}
}
public static void main(String args[]) {
try {
procA();
} catch (Exception e) {
[Link]("Exception caught");
}
procB();
procC();
}
}
Here is the output generated by the preceding program:
inside procA
procA’s finally
Exception caught
inside procB
procB’s finally
inside procC
procC’s finally
Categories of Exceptions
Checked exceptions −A checked exception is an exception that occurs
at the compiletime, these are also called as compile time exceptions.
These exceptions cannot simply be ignored at the time of compilation,
the programmer should take care of (handle) these exceptions.
Unchecked exceptions − An unchecked exception is an exception that
occurs at thetime of execution. These are also called as Runtime
Exceptions. These include programming bugs, such as logic errors or
improper use of an API. Runtime exceptions are ignored at the time of
compilation.
Common scenarios where exceptions may occur:
There are given some scenarios where unchecked exceptions can occur.
They are as follows:
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
2) Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by the
variable occurs an NullPointerException.
String s=null;
[Link]([Link]());//NullPointerException
3) Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result
ArrayIndexOutOfBoundsException as shown below:
int a[]=new int[5];
a[10]=50; //ArrayIndexOutOfBoundsException
Java’s Built-in Exceptions
User-defined Exceptions
All exceptions must be a child of Throwable.
If we want to write a checked exception that is automatically enforced by
the Handle ,we need to extend the Exception class.
User defined exception needs to inherit (extends) Exception class in
order to act as an exception.
throw keyword is used to throw such exceptions.
class MyOwnException extends Exception
{ public
MyOwnException(String
msg) { super(msg);
}
}
class EmployeeTest
{
static void employeeAge(int age) throws
MyOwnException {
if(age < 0)
throw new MyOwnException("Age can't be less
than zero"); else
[Link]("Input is valid!!");
}
public static void main(String[] args)
{
try { employeeAge(-2);
}
catch (MyOwnException e)
{
[Link]();
}
}
}
Advantages of Exception Handling
Exception handling allows us to control the normal flow of the program
by using exception handling in program.
It throws an exception whenever a calling method encounters an error
providing that the calling method takes care of that error.
It also gives us the scope of organizing and differentiating between
different error types using a separate block of codes. This is done with the
help of try-catch blocks.