Uploaded by Khant Si Thu

Chapter Five Exception

advertisement
Bahir Dar University
ባሕር ዳር ዩኒቨርሲቲ
Bahir Dar Institute of Technology
ባሕር ዳር ቴክኖሎጂ ኢንስቲትዩት
Faculty of Computing
ኮምፒዩትንግ ፋኩሊት
Object Oriented Programming
Course Code: SEng2071
Target Group: 2nd Year Software Engineering
Instructor: Haileyesus D.
Object Oriented Programming
CHAPTER FIVE:
EXCEPTION
Chapter Outline
Exception
 What is Exception?
 Reasons for Exception
 Categories of Exception
 Exception Hierarchy
 Built-in Exception
 Exception Handling
 try-catch
 The finally Block
 Throwing Exceptions
What is Exception?
Exception: is an unwanted or unexpected event, which occurs
during the execution of a program i.e. at run time, that disrupts the
normal flow of the program’s instructions.
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.
Reasons for Exception
An exception can occur for many different reasons, including the
following:
Dividing an integer by 0
A user has entered invalid data.
A file that needs to be opened cannot be found.
Accessing an element that is out of bounds in an array
Some of these exceptions are caused by user error, others by
programmer error, and others by physical resources that have failed
in some manner.
Categories of Exception
There are three categories of exceptions:
Checked exceptions
Unchecked exceptions
Errors
Checked Exception
Checked exceptions: is an exception that occurs at the compile time,
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.
For example, if you use FileReader class in your program to read data
from a file, if the file specified in its constructor doesn't exist, then a
FileNotFoundException occurs, and the compiler prompts the
programmer to handle the exception.
Checked Exception
FileNotFoundException
occurs
Unchecked Exception
Unchecked exceptions: is an exception that occurs at the time 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.
Example: If you have declared an array of size 4 in your program, and
trying to call the 6th element of the array then an
ArrayIndexOutOfBoundsException exception occurs.
Unchecked Exception
Example: If you have declared an array of size 4 in your program, and
trying to call the 6th element of the array then an
ArrayIndexOutOfBoundsException exception occurs.
Trying to access the 6th element of an
array
Error
Errors: These are not exceptions at all, but problems that arise
beyond the control of the user or the programmer.
Errors are typically ignored in your code because you can rarely do
anything about an error.
For example: if a stack overflow occurs, an error will arise
They are also ignored at the time of compilation.
Hierarchy of Exception
All exception classes are subtypes of the java.lang.Exception class.
The exception class is a subclass of the Throwable class.
Other than the exception class there is another subclass called Error
which is derived from the Throwable class.
Errors are abnormal conditions that happen in case of severe failures,
these are not handled by the Java programs.
Example: JVM is out of memory
Normally, programs cannot recover from errors.
Hierarchy of Exception
The Exception class has two main subclasses
IOException class and RuntimeException Class.
Following is a
list of most
common
checked and
unchecked
Java's Built-in
Exceptions
Built-in Exception
Java defines several exception classes inside the standard package
java.lang.
The most general of these exceptions are subclasses of the standard
type RuntimeException.
Since java.lang is implicitly imported into all Java programs, most
exceptions derived from RuntimeException are automatically
available.
Built-in Exception
Following is the list of Java Unchecked RuntimeException.
Built-in Exception
Following is the list of Java Unchecked RuntimeException.
Built-in Exception
Following is the list of Java Checked Exception.
Exception Handling
Exception handling enables you to create applications that can
resolve (or handle) exceptions.
Handling an exception allows a program to continue executing as if
no problem had been encountered.
Exception handling works by transferring the execution of a program
to an appropriate exception handler when an exception occurs.
Exception Handling
For example: if you call a method that opens a file but the file cannot
be opened, execution of that method will stop, and code that you
wrote to deal with this situation will be run.
Therefore, we need a way to tell the JVM what code to execute when
a certain exception happens.
To do this, we use the try and catch keywords.
Exception Handling
try {
// Put code here that might cause some kind of exception.
}
catch(ExceptionName parameterNname) {
// Put code here that handles this exception.
}
Try-catch
The code which is prone to exceptions is placed in the try block.
When an exception occurs, that exception occurred is handled by
catch block associated with it.
Every try block should be immediately followed either by a catch
block or finally block.
Try-catch
A catch statement: involves declaring the type of exception you are
trying to catch.
If an exception occurs in protected region, the catch block (or blocks)
that follows the try is checked.
If the type of exception that occurred is listed in a catch block, the
exception is passed to the catch block much as an argument is passed
into a method parameter.
Try-catch
Output
Try-catch
Multiple Catch Blocks
A try block can be followed by multiple catch blocks.
try {
// Put code here that might cause some kind of exception.
}
catch(ExceptionOne parameterNname) {
// Put code here that handles this exception.
}
catch(ExceptionTwo parameterNname) {
// Put code here that handles this exception.
}
Try-catch
Try-catch
The code that generate error
must be placed inside try
block
Try-catch
ArithmeticException
handler
Clear previous input
InputMismatchException
handler
The finally Block
The finally block: follows a try block or a catch block.
A finally block of code always executes, irrespective of occurrence of
an Exception.
Using a finally block allows you to run any cleanup-type statements
that you want to execute, no matter what happens in the protected
code(try block).
Even if there are multiple catch blocks, there is only one finally block.
A finally block can be added after a try block, if there are no catch
blocks. Or it can be added after the catch block(s).
The finally Block
try {
statement;
}
catch (Exception_type e) {
statement;
}
finally {
statement; //clean up  the finally block always executes
}
The finally Block
Throwing Exceptions
The classes of exception described above are all runtime exceptions.
Because these can happen often, they are automatically thrown by
methods.
These are also called unchecked exceptions
There are some types of exception that need to be explicitly thrown
by methods. These are called checked exceptions.
They are checked because the compiler checks to make sure they
have been declared or handled
Throwing Exceptions
Unchecked exceptions do not have to be declared or handled
An example of a checked exception is IOException.
This occurs when there are problems with reading or writing to files
or standard input/output.
If a method contains code that could generate an IOException, the
compiler will produce an error.
Throwing Exceptions
Example: Checked exception without declared, produce compile error
The attempt to read the
input could generate an
exception of type
IOException, which is a
checked exception. In
order to compile the
class, it must explicitly
declare that it throws
the exception, or handle
the exception.
Throwing Exceptions
To throw the exception, the method signature must include the
keyword throws and the exception type.
The exception is then thrown 'up' to whatever method invokes the
getInput() method.
Throwing Exceptions
Alternatively, the method getInput() needs to catch and handle the
exception itself.
Related documents
Download