0% found this document useful (0 votes)
7 views13 pages

Lec34-Exceptions

The document discusses exception handling in programming, defining exceptions as runtime anomalies that can be synchronous or asynchronous. It outlines various strategies for handling exceptions, including optimism, paranoia, and using try/catch/throw mechanisms. Additionally, it covers standard exception classes in C++, custom exceptions, and exception specifications for functions.

Uploaded by

Ghana Uni Help
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)
7 views13 pages

Lec34-Exceptions

The document discusses exception handling in programming, defining exceptions as runtime anomalies that can be synchronous or asynchronous. It outlines various strategies for handling exceptions, including optimism, paranoia, and using try/catch/throw mechanisms. Additionally, it covers standard exception classes in C++, custom exceptions, and exception specifications for functions.

Uploaded by

Ghana Uni Help
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/ 13

Fundamentals of Computer and Programming

Mohammad GANJTABESH

Department of Computer Science,


School of Mathematics, Statistics and Computer Science,
University of Tehran,
Tehran, Iran.

mgtabesh@ut.ac.ir

Dept. of Computer Science


University of Tehran
Exception Handling

Fundamentals of Computer and Programming


Undergraduate course

Mohammad GANJTABESH Dept. of Computer Science


mgtabesh@ut.ac.ir University of Tehran
Introduction

• Exceptions are run-time anomalies or unusual conditions encounter


during the execution of a program

• Two types of exceptions:


I Synchronous exceptions: occur during the program execution due to some
faults in input data (Division by zero, Out of range array access, Running
out of memory, etc.)
I Asynchronous exceptions: caused by external events or faults beyond the
control of the program (Keyboard/Hard disk failures, etc.)

• Synchronous exceptions could be handled in programming languages

1/9
Exception Handling Strategies
A) Optimism: Assume there are no errors!
B) Paranoia (Worst Case): Anticipate every possible mistake or source of
error (or as many as you can think of). Write lots of if statements
everywhere there may be a problem.

X
2/9
Exception Handling Strategies
A) Optimism: Assume there are no errors!
B) Paranoia (Worst Case): Anticipate every possible mistake or source of

Y
error (or as many as you can think of). Write lots of if statements
everywhere there may be a problem.
C) When it happens, we’ll fix it! Again, anticipate everything that might go

⌥ ⌅
wrong and just call assert in lots of places

assert # i n c l u d e<a s s e r t . h>


/ / # d e f i n e NDEBUG
I Used to test a condition during the execution: assert(condition); i n t main ( ) {
a s s e r t ( 2 + 2 == 4 ) ;
I If the condition is false, the program is terminated and an error c o u t << ” Checkpoint #1 ”<<e n d l ;
a s s e r t ( 2 * 2 == 4 ) ;
message is displayed c o u t << ” Checkpoint #2 ”<<e n d l ;

⌃ ⇧
return 0;
I Could be disabled by defining NDEBUG macro }

2/9
Exception Handling Strategies
A) Optimism: Assume there are no errors!
B) Paranoia (Worst Case): Anticipate every possible mistake or source of
error (or as many as you can think of). Write lots of if statements
everywhere there may be a problem.
C) When it happens, we’ll fix it! Again, anticipate everything that might go

⌥ ⌅
wrong and just call assert in lots of places

assert # i n c l u d e<a s s e r t . h>


/ / # d e f i n e NDEBUG
I Used to test a condition during the execution: assert(condition); i n t main ( ) {
a s s e r t ( 2 + 2 == 4 ) ;
I If the condition is false, the program is terminated and an error c o u t << ” Checkpoint #1 ”<<e n d l ;
a s s e r t ( 2 * 2 == 4 ) ;
message is displayed c o u t << ” Checkpoint #2 ”<<e n d l ;

⌃ ⇧
return 0;
I Could be disabled by defining NDEBUG macro }

D) Use exceptions!

2/9
Exception Handling
The goal is to create a routine that detects and sends an exceptional condition
in order to execute suitable actions:
1 Detect the problem (Hit the exception)
2 Inform that an error has been detected (Throw the exception)
3 Receive error information (Catch the exception)
4 Perform suitable actions (Handle the exception)

3/9
try/catch/throw
Exception handling mechanism basically builds upon three keywords:
• try: used to preface a block of statements which may generate exceptions
• catch: used to detect the exception and perform the appropriate actions
(must immediately follow the try block)
• throw: used to initiate an exception (any object could be thrown)
⌥ ⌅
t r y { / / Safe i n s t r u c t i o n s . When an e r r o r occurs :
/ / e x c e p t i o n i s thrown a u t o m a t i c a l l y
/ / o r throw an e x c e p t i o n manually
}
c a t c h ( ExceptionType e1 ) {
/ / catch block
}
c a t c h ( ExceptionType e2 ) {
/ / catch block
}
...
c a t c h ( ExceptionType eN) {
/ / catch block

⌃ ⇧
}
4/9
Example
⌥ ⌅⌥ ⌅
# i n c l u d e <iostream >
# i n c l u d e <math . h>
i n t main ( ) {
i n t x = 0 , y = 0;
double z = 0 ;
-
u s i n g namespace s t d ;
double d i v i s i o n ( i n t a , i n t b )
- c i n >> x >> y ;

S
try {
{
z = division (x , y );
2
i f ( b == 0 )
c o u t << z << e n d l ; =>
/ / e x c e p t i o n o f t y p e char * 10
0
c o u t << S q r t ( 1 0 0 ) << e n d l ;
.

throw ” D i v i s i o n by zero ! ” ;
c o u t << S q r t ( − 25) << e n d l ;
return (a/b ) ;
} c a t c h ( c o n s t char * msg ) {
}
c e r r << msg << e n d l ;
double S q r t ( double a )
} catch ( i n t e){
{
c e r r << ” Negative number ” << e ;
if ( a < 0 )
} catch ( . . . ) { / / catch everything
/ / exception of type i n t
c e r r << ” Unkown e x c e p t i o n ! ” ;
throw − 1;
}
return ( sqrt (a ) ) ; 3 while isr e t u r n
⌃ ⇧ ⌃}
0;


}

5/9
Standard Exception Classes
• Predefined in C++ (#include<stdexcept>)
• Guaranteed to be available in all versions
• Could be utilized as a base class for creating custom exceptions:
I In case of exception, override the virtual what() method accordingly
I For the others, customize the constructor

6/9
Example: Custom Exception
⌥ ⌅
c l a s s D i vi d e B yZ e r o E x c e p t i o n : p u b l i c e x c e p t i o n
{ ⌥ ⌅
public : # i n c l u d e <iostream>
c o n s t char * what ( ) throw ( ) # i n c l u d e <e x c e p t i o n>
{ u s i n g namespace s t d ;
r e t u r n ” D i v i s i o n by zero e x c e p t i o n ! ” ; i n t main ( )
} {
}; double a , b , c ;
c l a s s NegativeArgumentException : p u b l i c r u n t i m e e r r o r c i n >> a >> b >> c ;
{ try
public : {
NegativeArgumentException ( c o n s t s t r i n g & msg = ” ” ) cout<< d i v i s i o n ( a , b ) << e n d l ;

⌃ ⇧
: r u n t i m e e r r o r ( msg ) {} cout<< S q r t ( c ) << e n d l ;
}; }

⌥ ⌅
c a t c h ( D i v i d e B y Z e r o E x c e p t i o n& e )
{
c o u t << e . what ( ) << e n d l ;
double d i v i s i o n ( i n t a , i n t b ) }
{ c a t c h ( r u n t i m e e r r o r & e ) / / pass by r e f
i f ( b == 0 ) {
throw Di v i d e B y Z e r o E x c e p t i o n ( ) ; c o u t << e . what ( ) << e n d l ;
return (a/b ) ; }
} catch ( . . . )
double S q r t ( double a ) {
{ cout<<” Unkown e r r o r ! ” << e n d l ;

⌃ ⇧
if ( a < 0 ) }
throw NegativeArgumentException ( ” Negative argument ! ” ) ; }

⌃ ⇧
return ( sqrt (a ) ) ;
}
7/9
Exception Specification
Different types of exceptions could be specified for a function
• void function() throw ();
Promises that the function will not throw any exception
• void function() throw (int, DivideByZeroException, etc.);
Promises that the function may only throw the specified exceptions
Benefits: handling the specified exceptions would be sufficient
• void function();
⌥ ⌅
No promises (any type of exception might be thrown from this function)
double d i v i s i o n ( i n t a , i n t b ) throw ( Di vi deB yZe roE xc ept ion ) {
i f ( b == 0 )
throw Di vid eBy Ze r oE xce pt i on ( ) ; / / OK
return (a/b ) ;
}
double S q r t ( double a ) throw ( ) {
if ( a < 0 )
throw NegativeArgumentException ( ” Negative argument ! ” ) ; / / error
return ( sqrt (a ) ) ;

⌃ ⇧ 8/9
}
9/9

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