29-ExceptionHandling

29-ExceptionHandling - #include <iostream> #include...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <string> #include <sstream> // string stream using namespace std; /* General try/catch structure try { // user code/function calls that could // result in an exception being generated } catch (exception-type) { // code that executes when exception-type is thrown // in the try block } catch (exception-type) { // code that handles another exception type } */ void example1() { // Example 1 try { throw 15; } catch (int e) { cerr << "An exception was generated it's value is: " << e << endl; } // The first example throws an exception. When an exception is thrown // the system will look for a catch block that matches the type of // the thrown expression. In the above case an integer was thrown // so a similar action to function overloading occurs here, where // the throw acts as a function call to catch, whose footprint is // then matched up against our type } void example2() { try { throw 'A'; } catch (int e) { cerr << "An exception was generated it's value is: " << e << endl; } catch (char e) { cerr << "An exception was generated it's value is: " << e << endl; } // Matches with the char catch } void example3() { try { throw 'A'; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
catch (int e) { cerr << "An exception was generated it's value is: " << e << endl; } // whoops. We broke. The exception was not caught by us // so the system caught it instead } void example4() { try { throw 97; } catch (char e) { cerr << "An exception was generated it's value is: " << e << endl; } // still breaking, no implicit conversions being done here } void example5() { try { throw 97; } catch (long e) { cerr << "An exception was generated it's value is: " << e << endl; } // still not even converting between an int and a long, so our // similarity with function overloading has come to an end } void example6() { cout << "\nBefore the exception handling try block.\n"; try { throw 97l; } catch (long e) { cerr << "Our long exception type handler: " << e << endl; } cout << "After the catch block we go we go.\n"; } void example7() { cout << "\nBefore the exception handling try block.\n"; try { cout << "Before the exception we have some lovely code.\n"; throw 97l; cout << "After the exception I do not go.\n"; } catch (long e) { cerr << "Our long exception type handler: " << e << endl; } cout << "After the catch block we go we go.\n" << "Too bad we don't know, about the code from "
Background image of page 2
<< "after the throw.\n"; } //http://msdn2.microsoft.com/en-us/library/4t3saedz.aspx // The code block after the catch clause is the // exception handler, and catches (handles) the // exception thrown by the throw expression. // The exception-declaration statement indicates // the type of exception the clause handles. // The type can be any valid data type, including // a class. If the exception-declaration statement // is an ellipsis (. ..), the catch clause handles // any type of exception, including C exceptions // and system- or application-generated exceptions // such as memory protection, divide by zero, and // floating-point violations. Such a handler must
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/05/2011 for the course CS 37 taught by Professor Glenncuevas during the Summer '09 term at Irvine Valley College.

Page1 / 10

29-ExceptionHandling - #include &lt;iostream&gt; #include...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online