FP301 Chapter3 Exception - Handling

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Exception Handling

Concept of Exception Handling


Before we learn about exception handling,
let’s understand programming errors in Java
Programming Errors
Syntax Errors
Runtime Errors
Logic Errors
Syntax Errors
Errors that occur during compilation are called syntax
errors or compilation errors.
Syntax errors result from errors in code construction,
such as mistyping a keyword, omitting some necessary
punctuation, or using an opening brace without a
corresponding closing brace.
These errors are usually easy to detect, because the
compiler tells you where they are and what caused
them.
Syntax Errors (Cont..)
For example, compiling the following program results in a
syntax error, as shown in figure below:

Two errors are detected. Both are the result of not


declaring variable i. Since a single error will often display
many lines of compilation errors, it is a good practice to
start debugging from the top line and work downward.
Fixing errors that occur earlier in the program may also fix
Runtime Errors
Runtime errors are errors that cause a program to
terminate abnormally.
Runtime errors occur while an application is running
if the environment detects an operation that is
impossible to carry out.
Input errors are typical runtime errors.
An input error occurs when the user enters an
unexpected input value that the program cannot
handle.
Runtime Errors (Cont..)
For instance, if the program expects to read in a
number, but instead the user enters a string, this
causes data-type errors to occur in the program.
To prevent input errors, the program should prompt
the user to enter the correct type of values. It may
display a message like "Please enter an integer" before
reading an integer from the keyboard.
Another common source of runtime errors is division
by zero. This happens when the divisor is zero for
integer divisions.
Logic Errors
Logic errors occur when a program does not perform
the way it was intended to. Errors of this kind occur for
many different reasons.
For example, suppose you wrote the following
program to add number1 to number2.

The program does not have syntax errors or runtime


errors, but it does not print the correct result for
number2. See if you can find the error.
Concept of Exception
handling
Introduction to Exception Handling
An exception is an abnormal event that arises during the
execution of the program and disrupts the normal flow of the
program.
Abnormality do occur when your program is running. For
example, you might expect the user to enter an integer, but receive
a text string; or an unexpected I/O error pops up at runtime.
What really matters is "what happens after an abnormality
occurred?"
In other words, "how the abnormal situations are handled by your
program."
If these exceptions are not handled properly, the program
terminates abruptly and may cause severe consequences.
For example, the network connections, database connections and
files may remain opened; database and file records may be left in
an inconsistent state.
Intro to Exception Handling(cont..)
When an exception occurs, the normal execution flow
of the program will be interrupted.
Java provides programmers with the capability to
handle runtime exceptions.
With this capability, referred to as exception handling,
you can develop robust programs for mission-critical
computing.
Understanding Exception Handling
Java's exception-handling model is based on three
operations: declaring an exception, throwing an
exception, and catching an exception
Declaring Exceptions
In Java, the statement currently being executed belongs to a
method.
The Java interpreter invokes the main method for a Java application,
and the Web browser invokes the applet's no-arg constructor and
then the init method for a Java applet.
Every method must state the types of checked exceptions it might
throw. This is known as declaring exceptions.
Because system errors and runtime errors can happen to any code,
Java does not require that you declare Error and RuntimeException
(unchecked exceptions) explicitly in the method.
However, all other exceptions thrown by the method must be
explicitly declared in the method declaration so that the caller of
the method is informed of the exception.
Declaring Exceptions (Cont..)
To declare an exception in a method, use the throws
keyword in the method declaration, as in this example:

public void myMethod() throws IOException

The throws keyword indicates that myMethod might


throw an IOException. If the method might throw
multiple exceptions, add a list of the exceptions, separated
by commas, after throws:

public void myMethod() throws Exception1,


Exception2, ..., ExceptionN
Throwing Exceptions
 A program that detects an error can create an instance of an appropriate
exception type and throw it.
 This is known as throwing an exception.
 Here is an example: Suppose the program detected that an argument passed
to the method violates the method contract (e.g., the argument must be
non-negative, but a negative argument is passed); the program can create an
instance of IllegalArgumentException and throw it, as follows:

IllegalArgumentException ex = new IllegalArgumentException("Wrong


Argument");
throw ex;

 Or if you prefer, you can use the following:

throw new IllegalArgumentException("Wrong Argument");


Catching Exceptions
You now know how to declare an exception and how to
throw an exception. When an exception is thrown, it can
be caught and handled in a try-catch block, as follows:
Catching Exceptions(Cont..)
If no exceptions arise during the execution of the try
block, the catch blocks are skipped.
If one of the statements inside the try block throws an
exception, Java skips the remaining statements in the try
block and starts the process of finding the code to handle
the exception.
The process of finding a handler is called catching an
exception.
The finally Clause
Occasionally, you may want some code to be executed
regardless of whether an exception occurs or is caught.
Java has a finally clause that can be used to accomplish
this objective. The syntax for the finally clause might
look like this:
Exception Example

You might also like