diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog2/Codebeispiele/8_ch13 | |
| download | Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog2/Codebeispiele/8_ch13')
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp | 89 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp | 71 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp | 63 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp | 49 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp | 54 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp | 53 | ||||
| -rw-r--r-- | Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp | 86 |
7 files changed, 465 insertions, 0 deletions
diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp new file mode 100644 index 0000000..1d3ba0b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp @@ -0,0 +1,89 @@ +// Fig. 13.1: fig13_01.cpp
+// A simple exception-handling example that checks for
+// divide-by-zero exceptions.
+#include <iostream>
+
+using std::cout;
+using std::cin;
+using std::endl;
+
+#include <exception>
+
+using std::exception;
+
+// DivideByZeroException objects should be thrown by functions
+// upon detecting division-by-zero exceptions
+class DivideByZeroException : public exception {
+
+public:
+
+ // constructor specifies default error message
+ DivideByZeroException::DivideByZeroException()
+ : exception( "attempted to divide by zero" ) {}
+
+}; // end class DivideByZeroException
+
+// perform division and throw DivideByZeroException object if
+// divide-by-zero exception occurs
+double quotient( int numerator, int denominator )
+{
+ // throw DivideByZeroException if trying to divide by zero
+ if ( denominator == 0 )
+ throw DivideByZeroException(); // terminate function
+
+ // return division result
+ return static_cast< double >( numerator ) / denominator;
+
+} // end function quotient
+
+int main()
+{
+ int number1; // user-specified numerator
+ int number2; // user-specified denominator
+ double result; // result of division
+
+ cout << "Enter two integers (end-of-file to end): ";
+
+ // enable user to enter two integers to divide
+ while ( cin >> number1 >> number2 ) {
+
+ // try block contains code that might throw exception
+ // and code that should not execute if an exception occurs
+ try {
+ result = quotient( number1, number2 );
+ cout << "The quotient is: " << result << endl;
+
+ } // end try
+
+ // exception handler handles a divide-by-zero exception
+ catch ( DivideByZeroException ÷ByZeroException ) {
+ cout << "Exception occurred: " <<
+ divideByZeroException.what() << endl;
+
+ } // end catch
+
+ cout << "\nEnter two integers (end-of-file to end): ";
+
+ } // end while
+
+ cout << endl;
+
+ return 0; // terminate normally
+
+} // end main
+
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp new file mode 100644 index 0000000..f685afb --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp @@ -0,0 +1,71 @@ +// Fig. 13.2: fig13_02.cpp
+// Demonstrating exception rethrowing.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <exception>
+
+using std::exception;
+
+// throw, catch and rethrow exception
+void throwException()
+{
+ // throw exception and catch it immediately
+ try {
+ cout << " Function throwException throws an exception\n";
+ throw exception(); // generate exception
+
+ } // end try
+
+ // handle exception
+ catch ( exception &caughtException ) {
+ cout << " Exception handled in function throwException"
+ << "\n Function throwException rethrows exception";
+
+ throw; // rethrow exception for further processing
+
+ } // end catch
+
+ cout << "This also should not print\n";
+
+} // end function throwException
+
+int main()
+{
+ // throw exception
+ try {
+ cout << "\nmain invokes function throwException\n";
+ throwException();
+ cout << "This should not print\n";
+
+ } // end try
+
+ // handle exception
+ catch ( exception &caughtException ) {
+ cout << "\n\nException handled in main\n";
+
+ } // end catch
+
+ cout << "Program control continues after catch in main\n";
+
+ return 0;
+
+} // end main
+
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp new file mode 100644 index 0000000..c9147e8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp @@ -0,0 +1,63 @@ +// Fig. 13.3: fig13_03.cpp
+// Demonstrating stack unwinding.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <stdexcept>
+
+using std::runtime_error;
+
+// function3 throws run-time error
+void function3() throw ( runtime_error )
+{
+ throw runtime_error( "runtime_error in function3" ); // fourth
+}
+
+// function2 invokes function3
+void function2() throw ( runtime_error )
+{
+ function3(); // third
+}
+
+// function1 invokes function2
+void function1() throw ( runtime_error )
+{
+ function2(); // second
+}
+
+// demonstrate stack unwinding
+int main()
+{
+ // invoke function1
+ try {
+ function1(); // first
+
+ } // end try
+
+ // handle run-time error
+ catch ( runtime_error &error ) // fifth
+ {
+ cout << "Exception occurred: " << error.what() << endl;
+
+ } // end catch
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp new file mode 100644 index 0000000..34f588a --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp @@ -0,0 +1,49 @@ +// Fig. 13.4: fig13_04.cpp
+// Demonstrating pre-standard new returning 0 when memory
+// is not allocated.
+#include <iostream>
+
+using std::cout;
+
+int main()
+{
+ double *ptr[ 50 ];
+
+ // allocate memory for ptr
+ for ( int i = 0; i < 50; i++ ) {
+ ptr[ i ] = new double[ 5000000 ];
+
+ // new returns 0 on failure to allocate memory
+ if ( ptr[ i ] == 0 ) {
+ cout << "Memory allocation failed for ptr[ "
+ << i << " ]\n";
+
+ break;
+
+ } // end if
+
+ // successful memory allocation
+ else
+ cout << "Allocated 5000000 doubles in ptr[ "
+ << i << " ]\n";
+
+ } // end for
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp new file mode 100644 index 0000000..4fde5dd --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp @@ -0,0 +1,54 @@ +// Fig. 13.5: fig13_05.cpp
+// Demonstrating standard new throwing bad_alloc when memory
+// cannot be allocated.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <new> // standard operator new
+
+using std::bad_alloc;
+
+int main()
+{
+ double *ptr[ 50 ];
+
+ // attempt to allocate memory
+ try {
+
+ // allocate memory for ptr[ i ]; new throws bad_alloc
+ // on failure
+ for ( int i = 0; i < 50; i++ ) {
+ ptr[ i ] = new double[ 5000000 ];
+ cout << "Allocated 5000000 doubles in ptr[ "
+ << i << " ]\n";
+ }
+
+ } // end try
+
+ // handle bad_alloc exception
+ catch ( bad_alloc &memoryAllocationException ) {
+ cout << "Exception occurred: "
+ << memoryAllocationException.what() << endl;
+
+ } // end catch
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp new file mode 100644 index 0000000..b9656fb --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp @@ -0,0 +1,53 @@ +// Fig. 13.6: fig13_06.cpp
+// Demonstrating set_new_handler.
+#include <iostream>
+
+using std::cout;
+using std::cerr;
+
+#include <new> // standard operator new
+#include <cstdlib> // abort function prototype
+
+void customNewHandler()
+{
+ cerr << "customNewHandler was called";
+ abort();
+}
+
+// using set_new_handler to handle failed memory allocation
+int main()
+{
+ double *ptr[ 50 ];
+
+ // specify that customNewHandler should be called on failed
+ // memory allocation
+ set_new_handler( customNewHandler );
+
+ // allocate memory for ptr[ i ]; customNewHandler will be
+ // called on failed memory allocation
+ for ( int i = 0; i < 50; i++ ) {
+ ptr[ i ] = new double[ 5000000 ];
+
+ cout << "Allocated 5000000 doubles in ptr[ "
+ << i << " ]\n";
+
+ } // end for
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp new file mode 100644 index 0000000..975c0fc --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp @@ -0,0 +1,86 @@ +// Fig. 13.7: fig13_07.cpp
+// Demonstrating auto_ptr.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include <memory>
+
+using std::auto_ptr; // auto_ptr class definition
+
+class Integer {
+
+public:
+
+ // Integer constructor
+ Integer( int i = 0 )
+ : value( i )
+ {
+ cout << "Constructor for Integer " << value << endl;
+
+ } // end Integer constructor
+
+ // Integer destructor
+ ~Integer()
+ {
+ cout << "Destructor for Integer " << value << endl;
+
+ } // end Integer destructor
+
+ // functions to set Integer
+ void setInteger( int i )
+ {
+ value = i;
+
+ } // end function setInteger
+
+ // function to return Integer
+ int getInteger() const
+ {
+ return value;
+
+ } // end function getInteger
+
+private:
+ int value;
+
+}; // end class Integer
+
+// use auto_ptr to manipulate Integer object
+int main()
+{
+ cout << "Creating an auto_ptr object that points to an "
+ << "Integer\n";
+
+ // "aim" auto_ptr at Integer object
+ auto_ptr< Integer > ptrToInteger( new Integer( 7 ) );
+
+ cout << "\nUsing the auto_ptr to manipulate the Integer\n";
+
+ // use auto_ptr to set Integer value
+ ptrToInteger->setInteger( 99 );
+
+ // use auto_ptr to get Integer value
+ cout << "Integer after setInteger: "
+ << ( *ptrToInteger ).getInteger()
+ << "\n\nTerminating program" << endl;
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
+ * Hall. All Rights Reserved. *
+ * *
+ * DISCLAIMER: The authors and publisher of this book have used their *
+ * best efforts in preparing the book. These efforts include the *
+ * development, research, and testing of the theories and programs *
+ * to determine their effectiveness. The authors and publisher make *
+ * no warranty of any kind, expressed or implied, with regard to these *
+ * programs or to the documentation contained in these books. The authors *
+ * and publisher shall not be liable in any event for incidental or *
+ * consequential damages in connection with, or arising out of, the *
+ * furnishing, performance, or use of these programs. *
+ *************************************************************************/
\ No newline at end of file |
