0% found this document useful (0 votes)
17 views18 pages

19 Exceptions

The document discusses exceptions in programming, which are runtime errors that can cause a program to crash if not handled properly. It explains the concept of exception handling using keywords like try, throw, and catch, and provides examples of exception classes and their usage in code. Additionally, it illustrates how exceptions can carry arguments to provide more context about the error encountered.

Uploaded by

komal komal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

19 Exceptions

The document discusses exceptions in programming, which are runtime errors that can cause a program to crash if not handled properly. It explains the concept of exception handling using keywords like try, throw, and catch, and provides examples of exception classes and their usage in code. Additionally, it illustrates how exceptions can carry arguments to provide more context about the error encountered.

Uploaded by

komal komal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Exceptions

Exceptions
• Errors that occur at run time
• Examples
– Running out of memory
– Unable to open a file
– Divide by zero
– Assigning an invalid value
– Out of bounds index
– ….
Exception Handling
• If without handling,
• Program crashes
• Falls into unknown state
• An exception handler is a section of
program code that is designed to
execute when a particular exception
occurs
• Resolve the exception
• Lead to known state, such as
exiting the program
Exception Handling
• Keywords
– Try
– Throw
– Catch
Why we need Exceptions?
• C-language programs often signal an
error by returning a particular value
from the function in which it occurred.
• For example, disk-file functions often
return NULL or 0 to signal an error.
• Each time you call one of these
functions you check the return value.
Why we need Exceptions?
if( somefunc() == ERROR_RETURN_VALUE )
//handle the error or call error-handler function
else
//proceed normally

if( anotherfunc() == NULL )


//handle the error or call error-handler function
else
//proceed normally

if( thirdfunc() == 0 )
//handle the error or call error-handler function
else
//proceed normally
Exception Syntax
class AnError //exception class
{
};

class AClass //a class


{
public:

void Func() //a member function


{
if( /* error condition */ )
throw AnError(); //throw exception
}
};
Exception Syntax
void main() //application
{
try
{
AClass obj1;
obj1.Func(); //may cause error
}
catch(AnError e) //exception handler
{
//(catch block)
//tell user about error, etc.
}
}
Sequence of Events
1. Code is executing normally outside a try block.
2. Control enters the try block.
3. A statement in the try block causes an error in a
member function.
4. The member function throws an exception.
5. Control transfers to the exception handler (catch
block) following the try block.
Exceptions: Distance Class
class InchesEx { }; //exception class
class Distance
{
private:
int feet;
float inches;
public:

//-----------------------------------------------------------
Distance() //constructor (no args)
{ feet = 0; inches = 0.0; }

//-----------------------------------------------------------
Distance(int ft, float in) //constructor (two args)
{
if(in >= 12.0) //if inches too big,
throw InchesEx(); //throw exception
feet = ft;
inches = in;
void getdist() //get length from user
{
cout << "\nEnter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
if(inches >= 12.0) //if inches too big,
throw InchesEx(); //throw exception
}

//--------------------------------------------------

void showdist() //display distance


{
cout << feet << "\'-" << inches << "\'";
}

};
void main()
{
try
{
Distance dist1(17, 3.5);
Distance dist2;
dist2.getdist();
cout << "\ndist1 = "; dist1.showdist();
cout << "\ndist2 = "; dist2.showdist();
}
catch(InchesEx e) //catch exceptions
{
cout << "\nInitialization error: "
cout << “Inches value is too large.";
}

cout << endl;


getch();
}
Exceptions with Arguments

class Error {
private:
string message;

public:
Error(string s) { message = s; }
string getMessage() { return message; }

};
Exceptions with Arguments
class Rectangle {
private:
int l;
int w;
public:
Rectangle(int l, int w) {
if (l <= w)
throw Error("Length must be greater
than width");
this->l = l;
this->w = w;
}
};
Exceptions with Arguments

void main()
{

try {
Rectangle r(2, 10);
}
catch (Error e) {
cout << "Error:" << e.getMessage();
}
}
Exceptions with Arguments
class Distance
{
private:
int feet;
float inches;
public:

class InchesEx //exception class


{
public:
string origin;
float iValue;
InchesEx(string or, float in)
{
origin=or;
iValue = in;
}
}; //end of exception class
Distance(int ft = 0, float in = 0.0)
{
if(in >= 12.0)
throw InchesEx("Constructor", in);
feet = ft;
inches = in;
}
//------------------------------------------------
void getdist() //get length from user
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
if(inches >= 12.0)
throw InchesEx("getdist() function", inches);
}
//------------------------------------------------
void showdist() //display distance
{
cout << feet << "\'-" << inches << '\"';
}
};
void main()
{
try
{
Distance dist1(17, 3.5);
Distance dist2;
dist2.getdist();
cout << "\ndist1 = "; dist1.showdist();
cout << "\ndist2 = "; dist2.showdist();
}
catch(Distance::InchesEx ix) //exception handler
{
cout << "\nInitialization error in " <<
ix.origin << ".\n Inches value of " << ix.iValue
<< " is too large.";
}
cout << endl;
getch();
}

You might also like