SlideShare a Scribd company logo
ICS 313 - Fundamentals of Programming Languages 1
14. Exception Handling
14.1 Intro to Exception Handling
In a language without exception handling
When an exception occurs, control goes to the operating system, where a
message is displayed and the program is terminated
In a language with exception handling
Programs are allowed to trap some exceptions, thereby providing the
possibility of fixing the problem and continuing
Many languages allow programs to trap input / output errors (including
EOF)
An exception is any unusual event, either erroneous or not, detectable
by either hardware or software, that may require special processing
The special processing that may be required after detection of an
exception is called exception handling
The exception handling code unit is called an exception handler
ICS 313 - Fundamentals of Programming Languages 2
14.1 Intro to Exception Handling (continued)
An exception is raised when its associated event occurs
A language that does not have exception handling capabilities can
still define, detect, raise, and handle exceptions
Alternatives:
Send an auxiliary parameter or use the return value to indicate the
return status of a subprogram
Pass a label parameter to all subprograms (error return is to the
passed label)
Pass an exception handling subprogram to all subprograms
Advantages of Built-in Exception Handling:
Error detection code is tedious to write and it clutters the program
Exception propagation allows a high level of reuse of exception
handling code
14.1 Intro to Exception Handling (continued)
Design Issues for Exception Handling:
How and where are exception handlers specified and what is their
scope?
How is an exception occurrence bound to an exception handler?
Where does execution continue, if at all, after an exception handler
completes its execution?
How are user-defined exceptions specified?
Should there be default exception handlers for programs that do not
provide their own?
Can built-in exceptions be explicitly raised?
Are hardware-detectable errors treated as exceptions that can be
handled?
Are there any built-in exceptions?
How can exceptions be disabled, if at all?
ICS 313 - Fundamentals of Programming Languages 3
14.1 Intro to Exception Handling (continued)
Exception handling-control flow
14.4 Exception Handling in C++
Added to C++ in 1990
Design is based on that of CLU, Ada, and ML
Exception Handlers
Form:
try {
-- code that is expected to raise an exception
}
catch (formal parameter) {
-- handler code
}
...
catch (formal parameter) {
-- handler code
}
catch is the name of all handlers - it is an overloaded name, so the formal parameter of each must be
unique
The formal parameter need not have a variable - It can be simply a type name to distinguish the handler it
is in from others
The formal parameter can be used to transfer information to the handler
The formal parameter can be an ellipsis, in which case it handles all exceptions not yet handled
ICS 313 - Fundamentals of Programming Languages 4
14.4 Exception Handling in C++ (continued)
Binding Exceptions to Handlers
Exceptions are all raised explicitly by the statement:
throw [expression];
The brackets are metasymbols
A throw without an operand can only appear in a handler;
when it appears, it simply reraises the exception, which is then
handled elsewhere
The type of the expression disambiguates the intended handler
Unhandled exceptions are propagated to the caller of the
function in which it is raised
This propagation continues to the main function
If no handler is found, the program is terminated
14.4 Exception Handling in C++ (continued)
Continuation
After a handler completes its execution, control flows to the first statement
after the last handler in the sequence of handlers of which it is an element
Other Design Choices
All exceptions are user-defined
Exceptions are neither specified nor declared
Functions can list the exceptions they may raise - Without a specification,
a function can raise any exception (the throw clause)
See program listing (pp. 577-578)
Evaluation
It is odd that exceptions are not named and that hardware- and system
software-detectable exceptions cannot be handled
Binding exceptions to handlers through the type of the parameter certainly
does not promote readability
ICS 313 - Fundamentals of Programming Languages 5
14.5 Exception Handling in Java
Based on that of C++, but more in line with OOP philosophy
All exceptions are objects of classes that are descendants of
the Throwable class
The Java library includes two subclasses of Throwable :
Error
Thrown by the Java interpreter for events such as heap underflow
Never handled by user programs
Exception
User-defined exceptions are usually subclasses of this
Has two predefined subclasses, IOException and RuntimeException (e.g.,
ArrayIndexOutOfBoundsException and NullPointerException)
14.5 Exception Handling in Java (continued)
Java Exception Handlers
Like those of C++, except every catch requires a named
parameter and all parameters must be descendants of
Throwable
Syntax of try clause is exactly that of C++
Exceptions are thrown with throw, as in C++, but often the
throw includes the new operator to create the object, as in:
throw new MyException();
Binding an exception to a handler is simpler in Java than it is in
C++
An exception is bound to the first handler with a parameter is the same class as the
thrown object or an ancestor of it
An exception can be handled and rethrown by including a
throw in the handler (a handler could also throw a different
exception)
ICS 313 - Fundamentals of Programming Languages 6
14.5 Exception Handling in Java (continued)
Continuation
If no handler is found in the try construct, the search is
continued in the nearest enclosing try construct, etc.
If no handler is found in the method, the exception is
propagated to the method’s caller
If no handler is found (all the way to main), the
program is terminated
To insure that all exceptions are caught, a handler can be
included in any try construct that catches all exceptions
Simply use an Exception class parameter
Of course, it must be the last in the try construct
14.5 Exception Handling in Java (continued)
Other Design Choices
The Java throws clause is quite different from the throw clause of C++
Exceptions of class Error and RunTimeException and all of their
descendants are called unchecked exceptions
All other exceptions are called checked exceptions
Checked exceptions that may be thrown by a method must be either:
Listed in the throws clause, or
Handled in the method
A method cannot declare more exceptions in its throws clause than the method
it overrides
A method that calls a method that lists a particular checked exception in its
throws clause has three alternatives for dealing with that exception:
Catch and handle the exception
Catch the exception and throw an exception that is listed in its own throws clause
Declare it in its throws clause and do not handle it
ICS 313 - Fundamentals of Programming Languages 7
14.5 Exception Handling in Java (continued)
See Example program (pp. 582-583)
The finally Clause
Can appear at the end of a try construct
Purpose: To specify code that is to be executed, regardless of what
happens in the try construct
A try construct with a finally clause can be used outside
exception handling
Evaluation
The types of exceptions makes more sense than in the case of C++
The throws clause is better than that of C++ (The throw clause in
C++ says little to the programmer)
The finally clause is often useful
The Java interpreter throws a variety of exceptions that can be
handled by user programs

More Related Content

PPTX
Exception Handling in C++
PPSX
Exception Handling
PDF
Exception handling
PPTX
Exception handling
PPT
Exception handling and templates
PPTX
C++ ala
PPT
Exception handling in java
PPT
Exception handling
Exception Handling in C++
Exception Handling
Exception handling
Exception handling
Exception handling and templates
C++ ala
Exception handling in java
Exception handling

What's hot (20)

PPTX
Exception handling chapter15
PPTX
7.error management and exception handling
PPT
Exception handling
PPTX
What is Exception Handling?
PPT
Exception handler
PPT
Exceptions in c++
PPT
Week7 exception handling
PDF
Best Practices in Exception Handling
PPTX
130410107010 exception handling
PDF
Exception Handling in the C++ Constructor
PPTX
Exception handling in java
PPT
Exception Handling
PPTX
Exception handling
PPT
C# Exceptions Handling
PDF
Exception handling
PPTX
Exception handling c++
PPT
Exception Handling in JAVA
PPT
Exception handling
PPTX
Presentation1
ODP
Exception Handling In Java
Exception handling chapter15
7.error management and exception handling
Exception handling
What is Exception Handling?
Exception handler
Exceptions in c++
Week7 exception handling
Best Practices in Exception Handling
130410107010 exception handling
Exception Handling in the C++ Constructor
Exception handling in java
Exception Handling
Exception handling
C# Exceptions Handling
Exception handling
Exception handling c++
Exception Handling in JAVA
Exception handling
Presentation1
Exception Handling In Java
Ad

Similar to 14 exception handling (20)

PPT
Excetion handling Software Engineering Units
PPTX
Chapter 5
PPTX
L14 exception handling
PPTX
Chap2 exception handling
PPTX
Java-Unit 3- Chap2 exception handling
PPT
Chapter13 exception handling
PDF
Exception handling
PPTX
Exception handling
PPT
Exception and ErrorHandling in Java .ppt
PPT
Java Exception.ppt
PPTX
Exception handling in java
PPTX
Javasession4
PPTX
UNIT III 2021R.pptx
PPTX
UNIT III 2021R.pptx
DOCX
MODULE5_EXCEPTION HANDLING.docx
PPTX
Java -Exception handlingunit-iv
PPTX
EXCEPTION HANDLING in prograaming
PPTX
Lecture 3.1.1 Try Throw Catch.pptx
PPT
Exception Handling in java masters of computer application
ODP
Exception Handling In Java 15734
Excetion handling Software Engineering Units
Chapter 5
L14 exception handling
Chap2 exception handling
Java-Unit 3- Chap2 exception handling
Chapter13 exception handling
Exception handling
Exception handling
Exception and ErrorHandling in Java .ppt
Java Exception.ppt
Exception handling in java
Javasession4
UNIT III 2021R.pptx
UNIT III 2021R.pptx
MODULE5_EXCEPTION HANDLING.docx
Java -Exception handlingunit-iv
EXCEPTION HANDLING in prograaming
Lecture 3.1.1 Try Throw Catch.pptx
Exception Handling in java masters of computer application
Exception Handling In Java 15734
Ad

More from jigeno (20)

PDF
Access2007 part1
PDF
Basic introduction to ms access
PPSX
Bsit1
PDF
16 logical programming
PDF
15 functional programming
PDF
15 functional programming
PDF
13 concurrency
PDF
12 object oriented programming
PDF
11 abstract data types
PDF
9 subprograms
PDF
8 statement-level control structure
PDF
7 expressions and assignment statements
PDF
6 data types
PDF
5 names
PDF
4 lexical and syntax analysis
PDF
3 describing syntax and semantics
PDF
2 evolution of the major programming languages
PDF
1 preliminaries
PDF
Access2007 m2
PDF
Access2007 m1
Access2007 part1
Basic introduction to ms access
Bsit1
16 logical programming
15 functional programming
15 functional programming
13 concurrency
12 object oriented programming
11 abstract data types
9 subprograms
8 statement-level control structure
7 expressions and assignment statements
6 data types
5 names
4 lexical and syntax analysis
3 describing syntax and semantics
2 evolution of the major programming languages
1 preliminaries
Access2007 m2
Access2007 m1

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
Teaching material agriculture food technology
PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
The AUB Centre for AI in Media Proposal.docx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
Understanding_Digital_Forensics_Presentation.pptx
Teaching material agriculture food technology
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
Chapter 3 Spatial Domain Image Processing.pdf
sap open course for s4hana steps from ECC to s4
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Empathic Computing: Creating Shared Understanding

14 exception handling

  • 1. ICS 313 - Fundamentals of Programming Languages 1 14. Exception Handling 14.1 Intro to Exception Handling In a language without exception handling When an exception occurs, control goes to the operating system, where a message is displayed and the program is terminated In a language with exception handling Programs are allowed to trap some exceptions, thereby providing the possibility of fixing the problem and continuing Many languages allow programs to trap input / output errors (including EOF) An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing The special processing that may be required after detection of an exception is called exception handling The exception handling code unit is called an exception handler
  • 2. ICS 313 - Fundamentals of Programming Languages 2 14.1 Intro to Exception Handling (continued) An exception is raised when its associated event occurs A language that does not have exception handling capabilities can still define, detect, raise, and handle exceptions Alternatives: Send an auxiliary parameter or use the return value to indicate the return status of a subprogram Pass a label parameter to all subprograms (error return is to the passed label) Pass an exception handling subprogram to all subprograms Advantages of Built-in Exception Handling: Error detection code is tedious to write and it clutters the program Exception propagation allows a high level of reuse of exception handling code 14.1 Intro to Exception Handling (continued) Design Issues for Exception Handling: How and where are exception handlers specified and what is their scope? How is an exception occurrence bound to an exception handler? Where does execution continue, if at all, after an exception handler completes its execution? How are user-defined exceptions specified? Should there be default exception handlers for programs that do not provide their own? Can built-in exceptions be explicitly raised? Are hardware-detectable errors treated as exceptions that can be handled? Are there any built-in exceptions? How can exceptions be disabled, if at all?
  • 3. ICS 313 - Fundamentals of Programming Languages 3 14.1 Intro to Exception Handling (continued) Exception handling-control flow 14.4 Exception Handling in C++ Added to C++ in 1990 Design is based on that of CLU, Ada, and ML Exception Handlers Form: try { -- code that is expected to raise an exception } catch (formal parameter) { -- handler code } ... catch (formal parameter) { -- handler code } catch is the name of all handlers - it is an overloaded name, so the formal parameter of each must be unique The formal parameter need not have a variable - It can be simply a type name to distinguish the handler it is in from others The formal parameter can be used to transfer information to the handler The formal parameter can be an ellipsis, in which case it handles all exceptions not yet handled
  • 4. ICS 313 - Fundamentals of Programming Languages 4 14.4 Exception Handling in C++ (continued) Binding Exceptions to Handlers Exceptions are all raised explicitly by the statement: throw [expression]; The brackets are metasymbols A throw without an operand can only appear in a handler; when it appears, it simply reraises the exception, which is then handled elsewhere The type of the expression disambiguates the intended handler Unhandled exceptions are propagated to the caller of the function in which it is raised This propagation continues to the main function If no handler is found, the program is terminated 14.4 Exception Handling in C++ (continued) Continuation After a handler completes its execution, control flows to the first statement after the last handler in the sequence of handlers of which it is an element Other Design Choices All exceptions are user-defined Exceptions are neither specified nor declared Functions can list the exceptions they may raise - Without a specification, a function can raise any exception (the throw clause) See program listing (pp. 577-578) Evaluation It is odd that exceptions are not named and that hardware- and system software-detectable exceptions cannot be handled Binding exceptions to handlers through the type of the parameter certainly does not promote readability
  • 5. ICS 313 - Fundamentals of Programming Languages 5 14.5 Exception Handling in Java Based on that of C++, but more in line with OOP philosophy All exceptions are objects of classes that are descendants of the Throwable class The Java library includes two subclasses of Throwable : Error Thrown by the Java interpreter for events such as heap underflow Never handled by user programs Exception User-defined exceptions are usually subclasses of this Has two predefined subclasses, IOException and RuntimeException (e.g., ArrayIndexOutOfBoundsException and NullPointerException) 14.5 Exception Handling in Java (continued) Java Exception Handlers Like those of C++, except every catch requires a named parameter and all parameters must be descendants of Throwable Syntax of try clause is exactly that of C++ Exceptions are thrown with throw, as in C++, but often the throw includes the new operator to create the object, as in: throw new MyException(); Binding an exception to a handler is simpler in Java than it is in C++ An exception is bound to the first handler with a parameter is the same class as the thrown object or an ancestor of it An exception can be handled and rethrown by including a throw in the handler (a handler could also throw a different exception)
  • 6. ICS 313 - Fundamentals of Programming Languages 6 14.5 Exception Handling in Java (continued) Continuation If no handler is found in the try construct, the search is continued in the nearest enclosing try construct, etc. If no handler is found in the method, the exception is propagated to the method’s caller If no handler is found (all the way to main), the program is terminated To insure that all exceptions are caught, a handler can be included in any try construct that catches all exceptions Simply use an Exception class parameter Of course, it must be the last in the try construct 14.5 Exception Handling in Java (continued) Other Design Choices The Java throws clause is quite different from the throw clause of C++ Exceptions of class Error and RunTimeException and all of their descendants are called unchecked exceptions All other exceptions are called checked exceptions Checked exceptions that may be thrown by a method must be either: Listed in the throws clause, or Handled in the method A method cannot declare more exceptions in its throws clause than the method it overrides A method that calls a method that lists a particular checked exception in its throws clause has three alternatives for dealing with that exception: Catch and handle the exception Catch the exception and throw an exception that is listed in its own throws clause Declare it in its throws clause and do not handle it
  • 7. ICS 313 - Fundamentals of Programming Languages 7 14.5 Exception Handling in Java (continued) See Example program (pp. 582-583) The finally Clause Can appear at the end of a try construct Purpose: To specify code that is to be executed, regardless of what happens in the try construct A try construct with a finally clause can be used outside exception handling Evaluation The types of exceptions makes more sense than in the case of C++ The throws clause is better than that of C++ (The throw clause in C++ says little to the programmer) The finally clause is often useful The Java interpreter throws a variety of exceptions that can be handled by user programs