SlideShare una empresa de Scribd logo
1 de 7
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

Más contenido relacionado

La actualidad más candente

Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handlingDeepak Sharma
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Best Practices in Exception Handling
Best Practices in Exception HandlingBest Practices in Exception Handling
Best Practices in Exception HandlingLemi Orhan Ergin
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handlingHemant Chetwani
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ ConstructorSomenath Mukhopadhyay
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling sharqiyem
 
Exception handling
Exception handlingException handling
Exception handlingRavi Sharda
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVASURIT DATTA
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Javaparag
 

La actualidad más candente (20)

Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
Exception handler
Exception handler Exception handler
Exception handler
 
Exceptions in c++
Exceptions in c++Exceptions in c++
Exceptions in c++
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Best Practices in Exception Handling
Best Practices in Exception HandlingBest Practices in Exception Handling
Best Practices in Exception Handling
 
130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
C# Exceptions Handling
C# Exceptions Handling C# Exceptions Handling
C# Exceptions Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Exception handling
Exception handlingException handling
Exception handling
 
Presentation1
Presentation1Presentation1
Presentation1
 
Exception Handling In Java
Exception Handling In JavaException Handling In Java
Exception Handling In Java
 

Similar a 14 exception handling

Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingSakkaravarthiS1
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertionsphanleson
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.netsuraj pandey
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception HandlingAshwin Shiv
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-ivRubaNagarajan
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]ppd1961
 
Exception handling
Exception handlingException handling
Exception handlingzindadili
 

Similar a 14 exception handling (20)

Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Java exception
Java exception Java exception
Java exception
 
UNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and MultithreadingUNIT-3.pptx Exception Handling and Multithreading
UNIT-3.pptx Exception Handling and Multithreading
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertions
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Introduction of exception in vb.net
Introduction of exception in vb.netIntroduction of exception in vb.net
Introduction of exception in vb.net
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
$Cash
$Cash$Cash
$Cash
 
$Cash
$Cash$Cash
$Cash
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Java -Exception handlingunit-iv
Java -Exception handlingunit-ivJava -Exception handlingunit-iv
Java -Exception handlingunit-iv
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
6-Error Handling.pptx
6-Error Handling.pptx6-Error Handling.pptx
6-Error Handling.pptx
 
Java unit 11
Java unit 11Java unit 11
Java unit 11
 
Introduction to Exception
Introduction to ExceptionIntroduction to Exception
Introduction to Exception
 
Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]Handling Exceptions In C & C++[Part A]
Handling Exceptions In C & C++[Part A]
 
Exception handling
Exception handlingException handling
Exception handling
 

Más de jigeno

Access2007 part1
Access2007 part1Access2007 part1
Access2007 part1jigeno
 
Basic introduction to ms access
Basic introduction to ms accessBasic introduction to ms access
Basic introduction to ms accessjigeno
 
16 logical programming
16 logical programming16 logical programming
16 logical programmingjigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programmingjigeno
 
15 functional programming
15 functional programming15 functional programming
15 functional programmingjigeno
 
13 concurrency
13 concurrency13 concurrency
13 concurrencyjigeno
 
12 object oriented programming
12 object oriented programming12 object oriented programming
12 object oriented programmingjigeno
 
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data typesjigeno
 
9 subprograms
9 subprograms9 subprograms
9 subprogramsjigeno
 
8 statement-level control structure
8 statement-level control structure8 statement-level control structure
8 statement-level control structurejigeno
 
7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statementsjigeno
 
6 data types
6 data types6 data types
6 data typesjigeno
 
5 names
5 names5 names
5 namesjigeno
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysisjigeno
 
3 describing syntax and semantics
3 describing syntax and semantics3 describing syntax and semantics
3 describing syntax and semanticsjigeno
 
2 evolution of the major programming languages
2 evolution of the major programming languages2 evolution of the major programming languages
2 evolution of the major programming languagesjigeno
 
1 preliminaries
1 preliminaries1 preliminaries
1 preliminariesjigeno
 
Access2007 m2
Access2007 m2Access2007 m2
Access2007 m2jigeno
 
Access2007 m1
Access2007 m1Access2007 m1
Access2007 m1jigeno
 

Más de jigeno (20)

Access2007 part1
Access2007 part1Access2007 part1
Access2007 part1
 
Basic introduction to ms access
Basic introduction to ms accessBasic introduction to ms access
Basic introduction to ms access
 
Bsit1
Bsit1Bsit1
Bsit1
 
16 logical programming
16 logical programming16 logical programming
16 logical programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
15 functional programming
15 functional programming15 functional programming
15 functional programming
 
13 concurrency
13 concurrency13 concurrency
13 concurrency
 
12 object oriented programming
12 object oriented programming12 object oriented programming
12 object oriented programming
 
11 abstract data types
11 abstract data types11 abstract data types
11 abstract data types
 
9 subprograms
9 subprograms9 subprograms
9 subprograms
 
8 statement-level control structure
8 statement-level control structure8 statement-level control structure
8 statement-level control structure
 
7 expressions and assignment statements
7 expressions and assignment statements7 expressions and assignment statements
7 expressions and assignment statements
 
6 data types
6 data types6 data types
6 data types
 
5 names
5 names5 names
5 names
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
 
3 describing syntax and semantics
3 describing syntax and semantics3 describing syntax and semantics
3 describing syntax and semantics
 
2 evolution of the major programming languages
2 evolution of the major programming languages2 evolution of the major programming languages
2 evolution of the major programming languages
 
1 preliminaries
1 preliminaries1 preliminaries
1 preliminaries
 
Access2007 m2
Access2007 m2Access2007 m2
Access2007 m2
 
Access2007 m1
Access2007 m1Access2007 m1
Access2007 m1
 

Último

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 

Último (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 

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