Lec34-Exceptions
Lec34-Exceptions
Mohammad GANJTABESH
mgtabesh@ut.ac.ir
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
⌃ ⇧
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
⌃ ⇧
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