0% found this document useful (0 votes)
4 views

Lecture7

This document covers exception handling in object-oriented programming, detailing how to improve code reliability through exception-handling mechanisms. It explains the definition of exceptions, the use of try-catch blocks, and the distinction between checked and unchecked exceptions. Additionally, it discusses propagating exceptions, throwing exceptions, and the different types of exception throwers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture7

This document covers exception handling in object-oriented programming, detailing how to improve code reliability through exception-handling mechanisms. It explains the definition of exceptions, the use of try-catch blocks, and the distinction between checked and unchecked exceptions. Additionally, it discusses propagating exceptions, throwing exceptions, and the different types of exception throwers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Faculty of Information Technology

Fall 2020/21

Object-Oriented Programming
CS-201, CS201, C212

Lec. (7)
Chapter 8

Exceptions
Objectives
After you have read and studied this chapter, you should be able to
Improve the reliability of code by incorporating exception-handling and assertion mechanisms.
Write methods that propagate exceptions.
Implement the try-catch blocks for catching and handling exceptions.
Write programmer-defined exception classes.
Distinguish the checked and unchecked, or runtime, exceptions.
Definition

An exception represents an error condition that can occur during the normal
course of program execution.

When an exception occurs, or is thrown, the normal sequence of flow is


terminated. The exception-handling routine is then executed; we say the thrown
exception is caught.
Not Catching Exceptions

String inputStr;
int age;

inputStr = JOptionPane.showInputDialog(null, "Age:");


age = Integer.parseInt(inputStr);

Error message for invalid input


java.lang.NumberFormatException: ten
at java.lang.Integer.parseInt(Integer.java:405)
at java.lang.Integer.parseInt(Integer.java:454)
at Ch8Sample1.main(Ch8Sample1.java:20)
Catching an Exception

inputStr = JOptionPane.showInputDialog(null, "Age:");

try {

try age = Integer.parseInt(inputStr);

} catch (NumberFormatException e){

JOptionPane.showMessageDialog(null, "’" + inputStr


catch + "‘ is invalid\n"
+ "Please enter digits only");
}
try-catch Control Flow
Exception No Exception
try { try {
<t-stmt-1> <t-stmt-1>
Assume <t-stmt-3>
<t-stmt-2> throws an exception. <t-stmt-2> All statements in
the try block are
<t-stmt-3> <t-stmt-3>
executed.
<t-stmt-4> Remaining <t-stmt-4>
. . . statements in the . . .
try block is skipped.
<t-stmt-n> <t-stmt-n>

} catch (Exception e) { } catch (Exception e) {


<c-stmt-1> <c-stmt-1>
Statements in the Statements in the
. . . catch block are . . . catch block are
<c-stmt-m> executed. <c-stmt-m> skipped.
} }
And the execution
<next stmt> continues to the <next stmt>
next statement
Getting Information

There are two methods we can call to get information about the thrown
exception:
getMessage
printStackTrace

try {
. . .
} catch (NumberFormatException e){

System.out.println(e.getMessage());
System.out.println(e.printStackTrace());
}
Multiple catch Blocks
A single try-catch statement can include multiple catch blocks, one
for each type of exception.
try {
. . .
age = Integer.parseInt(inputStr);
. . .
val = cal.get(id); //cal is a GregorianCalendar
. . .
} catch (NumberFormatException e){
. . .
} catch (ArrayIndexOutOfBoundsException e){
. . .
}
Multiple catch Control Flow
Exception No Exception
try { Assume <t-stmt-3> try {
throws an exception
<t-stmt-1> and <catch-block-3> <t-stmt-1>
<t-stmt-2> is the matching block. <t-stmt-2> All statements in
the try block are
<t-stmt-3> <t-stmt-3>
executed and throw
<t-stmt-4> Remaining <t-stmt-4> no exceptions.
. . . statements in the . . .
try block is skipped.
<t-stmt-n> <t-stmt-n>
} }
<catch-block-1> <catch-block-1>
<catch-block-2> Statements <catch-block-2> All catch
in the blocks are
<catch-block-3> <catch-block-3> skipped.
matching
. . . catch block . . .
<catch-block-m> are executed. <catch-block-m>
} }
<next stmt> <next stmt>
The finally Block

There are situations where we need to take certain actions regardless of whether
an exception is thrown or not.
We place statements that must be executed regardless of exceptions in the finally
block.
try-catch-finally Control Flow
Exception No Exception
try { Assume <t-stmt-i> try {
throws an exception
<t-stmt-1> <t-stmt-1>
and <catch-block-i> is
. . . the matching block. . . .
<t-stmt-i> <t-stmt-i>
. . . . . .
<t-stmt-n> <t-stmt-n>
} }
<catch-block-1> <catch-block-1>
. . . . . .
<catch-block-i> <catch-block-i>
. . . . . .
<catch-block-m> <catch-block-m>

} finally { } finally {
. . . finally block is . . . finally block is
} executed. } executed.
<next stmt> <next stmt>
Propagating Exceptions
Instead of catching a thrown exception by using the try-catch
statement, we can propagate the thrown exception back to the
caller of our method.
The method header includes the reserved word throws.

public int getAge( ) throws NumberFormatException {


. . .
int age = Integer.parseInt(inputStr);
. . .
return age;
}
Throwing Exceptions
We can write a method that throws an exception directly, i.e., this
method is the origin of the exception.
Use the throw reserved to create a new instance of the Exception
or its subclasses.
The method header includes the reserved word throws.

public void doWork(int num) throws Exception {


. . .
if (num != val) throw new Exception("Invalid val");
. . .
}
Exception Thrower

When a method may throw an exception, either directly or indirectly,


we call the method an exception thrower.

Every exception thrower must be one of two types:


catcher.
propagator.
Types of Exception Throwers

An exception catcher is an exception thrower that includes a matching catch


block for the thrown exception.

An exception propagator does not contain a matching catch block.

A method may be a catcher of one exception and a propagator of another.


Sample Call Sequence

Method A Method B Method C Method D


try { try { try {
B(); C(); if (cond) {
} catch (Exception e){ } catch (Exception e){ D(); throw
. . . . . . } catch (Exception e){ new Exception();
} } . . .

catcher propagator propagator

D
C C
B B B
A A A A

Stack Trace
Exception Types

All types of thrown errors are instances of the Throwable class or its sub
Serious errors are represented by instances of the Error class or its subclasses.
Exceptional cases that common applications should handle are represented by instanc
Exception class or its subclasses.
Throwable Hierarchy

There are over 60 classes in the hierarchy.


Checked vs. Runtime

There are two types of exceptions:


Checked.
Unchecked.
A checked exception is an exception that is checked at compile time.
All other exceptions are unchecked, or runtime, exceptions.
As the name suggests, they are detected only at runtime.
Different Handling Rules

When calling a method that can throw checked exceptions


use the try-catch statement and place the call in the try block, or
modify the method header to include the appropriate throws clause.

When calling a method that can throw runtime exceptions, it is optional to use
the try-catch statement or modify the method header to include a throws clause.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy