Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 18

Chapter 18

Exception Handling

This activity contains 10 questions.

Question 1.

In your current directory you do not have a file named "foo.txt". What will the following program output?

void main()
{
  ifstream f;
  int num;
  int x=0;

  try {
	  f.open("foo.txt");
	  if (f.fail()) {
		  throw(1);
	  }
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num;
		cout << num << endl;
	  }
	  f.close();	  
  }
  catch (int x) {
	  cout << "The error is " << x << endl;
  }
}
 
End of Question 1


Question 2.

What is the error in the following code?

void main()
{
  ifstream f;
  int num1, num2, val;

  try {
	  f.open("foo.txt");
	  if (f.fail()) {
		  throw(0);
	  }
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		if (num2==0) {
			throw('x');
		}
		val = num1/num2;
		cout << val << endl;
	  }
	  f.close();	  
  }
  catch (...) {
	  cout << "There was an error." << endl;
  }
  catch (int x) {
	  cout << "The error code is " << x << endl;
  }
}
 
End of Question 2


Question 3.

Given the following code, how could we write the function openFile so that it correctly throws an exception if the file does not open correctly?

void main()
{
  ifstream f;
  int num1, num2, val;

  try {
	  openFile(f);
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		if (num2==0) {
			throw("divide by zero");
		}
		val = num1/num2;
		cout << val << endl;
	  }
	  f.close();	  
  }  
  catch (char *s) {
	  cout << "There was error: " << s << endl;
  }  
}
 
End of Question 3


Question 4.

You do not have a file named "foo.txt" in the current directory. What will the following program output when run?

void openFile(ifstream &f) throw(char *)
{
    f.open("foo.txt");    
    if (f.fail()) {
	  throw("File open failure");
   }
}

void main()
{
  ifstream f;
  int val;

  openFile(f);
  cout << "The numbers are:" << endl;
  while (!f.eof()) {
	f >> val;
	cout << val << endl;
  }
  f.close();	   
}
 
End of Question 4


Question 5.

In your current directory you have a file named "foo.txt" that contains:

30
10
40
0

What will the following program output?

void openFile(ifstream &f) throw(char *)
{
	f.open("foo.txt");    
    if (f.fail()) {
	  throw("File open failure");
	}
}

void main()
{
  ifstream f;
  int num1, num2, val;
  int x=1;

  try {
	  openFile(f);
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		if (num2==0) {
			throw(0);
		}
		val = num1/num2;
		cout << val << endl;
	  }
	  f.close();	  
  }  
  catch (char *s) {
	  cout << "There was error: " << s << endl;
  }  
  catch (int x) {
	  cout << "Divide by zero?  code = " << x << endl;
  }
}
 
End of Question 5


Question 6.

Given the following code, what is a valid definition for the openFile function?

// Base class Exception
class Exception
{
public:
	string error;
	Exception();
	Exception(string s);
};

Exception::Exception()
{
	error = "Unknown";
}

Exception::Exception(string s)
{
	error = s;
}

// Class Divide by Zero Exception
class DivideZeroException : public Exception
{
public:
	DivideZeroException(string s);
};

DivideZeroException::DivideZeroException(string s) :
	Exception(s)
{
}


// Class File Exception
class FileException : public Exception
{
public:
	FileException(string s);
};

FileException::FileException(string s) :
	Exception(s)
{
}

void openFile(ifstream &f) throw(FileException)
{		
    f.open("foo.txt");    
    if (f.fail()) {
	  throw(FileException("File open failed"));
    }
}

// Main
void main()
{
  ifstream f;
  int num1, num2, val;
  int x=1;

  try {
	  openFile(f);
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		if (num2==0)
			throw(DivideZeroException("Divide by zero"));
		}
		val = num1/num2;
		cout << val << endl;
	  }
	  f.close();	  
  }  
  catch (FileException e) {
	  cout << "There was error: " << e.error << endl;
  }  
  catch (DivideZeroException e) {
	  cout << "There was error: " << e.error << endl;
  }
}
 
End of Question 6


Question 7.

In your current directory you have a file named "foo.txt" that contains:

30
10
40
0

What will the following program output?

// Base class Exception
class Exception
{
public:
	string error;
	Exception();
	Exception(string s);
};

Exception::Exception()
{
	error = "Unknown";
}

Exception::Exception(string s)
{
	error = s;
}

// Class Divide by Zero Exception
class DivideZeroException : public Exception
{
public:
	DivideZeroException(string s);
};

DivideZeroException::DivideZeroException(string s) :
	Exception(s)
{
}


// Class File Exception
class FileException : public Exception
{
public:
	FileException(string s);
};

FileException::FileException(string s) :
	Exception(s)
{
}

void openFile(ifstream &f) throw(FileException)
{		
	f.open("foo.txt");    
	if (f.fail()) {
		throw(FileException("File open failed"));
	}
}

// Main
void main()
{
  ifstream f;
  int num1, num2, val;
  int x=1;

  try {
	  openFile(f);
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		if (num2==0) {
		 throw(
		  DivideZeroException("Divide by zero"));
		}
		val = num1/num2;
		cout << val << endl;
	  }
	  f.close();	  
  }
  catch (Exception e) {
	  cout << "There was error: " << e.error << endl;
  }
}
 
End of Question 7


Question 8.

In your current directory you have a file named "foo.txt" that contains:

30
10
40
0

What will the following program output?

// Base class Exception
class Exception
{
public:
	string error;
	Exception();
	Exception(string s);
};

Exception::Exception()
{
	error = "Unknown";
}

Exception::Exception(string s)
{
	error = s;
}

// Class Divide by Zero Exception
class DivideZeroException : public Exception
{
public:
	DivideZeroException(string s);
};

DivideZeroException::DivideZeroException(string s) :
	Exception(s)
{
}


// Class File Exception
class FileException : public Exception
{
public:
	FileException(string s);
};

FileException::FileException(string s) :
	Exception(s)
{
}

void openFile(ifstream &f) throw(FileException)
{		
	f.open("foo.txt");    
	if (f.fail()) {
		throw(FileException("File open failed"));
	}
}

// Main
void main()
{
  ifstream f;
  int num1, num2, val;
  int x=1;

  try {
	  openFile(f);
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		if (num2==0) {
		 throw(
		  DivideZeroException("Divide by zero"));
		}
		val = num1/num2;
		cout << val << endl;
	  }
	  f.close();	  
  }
  catch (Exception) {
	  cout << "There was an error." << endl;
  }
  catch (FileException e) {
	  cout << "There was a file error: " << e.error << endl;
  }  
  catch (DivideZeroException e) {
	  cout << "There was a division error: " << e.error << endl;
  } 
}
 
End of Question 8


Question 9.
If you have already defined a class named "MyClass", which of the following code snippets will throw an exception if there is insufficient memory available to allocate a new node?

 
End of Question 9


Question 10.

In your current directory you have a file named "foo.txt" that contains:

30
10
40
0
60
30

What will the following program output?

// Base class Exception
class Exception
{
public:
	string error;
	Exception();
	Exception(string s);
};

Exception::Exception()
{
	error = "Unknown";
}

Exception::Exception(string s)
{
	error = s;
}

// Class Divide by Zero Exception
class DivideZeroException : public Exception
{
public:
	DivideZeroException(string s);
};

DivideZeroException::DivideZeroException(string s) :
	Exception(s)
{
}

// Class File Exception
class FileException : public Exception
{
public:
	FileException(string s);
};

FileException::FileException(string s) :
	Exception(s)
{
}

void openFile(ifstream &f) throw(FileException)
{		
	f.open("foo.txt");    
	if (f.fail()) {
		throw(FileException("File open failed"));
	}
}

// Main
void main()
{
  ifstream f;
  int num1, num2, val;
  int x=1;

  try {
	  openFile(f);
	  cout << "The numbers are:" << endl;
	  while (!f.eof()) {
		f >> num1;
		f >> num2;
		try {
			if (num2==0) {
			 throw(
			  DivideZeroException("Divide by zero"));
			}
			val = num1/num2;
			cout << val << endl;
		}
	    catch (DivideZeroException e) {
			cout << "There was a division error: " 
                             << e.error << endl;
		} 
	  }
	  f.close();	  
  }
  catch (FileException e) {
	  cout << "There was a file error: " << e.error << endl;
  } 
}
 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page