You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stdcxx.apache.org by se...@apache.org on 2007/06/04 22:22:28 UTC
svn commit: r544245 - /incubator/stdcxx/trunk/doc/stdlibug/18-4.html
Author: sebor
Date: Mon Jun 4 13:22:27 2007
New Revision: 544245
URL: http://svn.apache.org/viewvc?view=rev&rev=544245
Log:
2007-06-04 Martin Sebor <se...@roguewave.com>
* 18-4.html: Updated example program to match the latest code
and added possible output.
Modified:
incubator/stdcxx/trunk/doc/stdlibug/18-4.html
Modified: incubator/stdcxx/trunk/doc/stdlibug/18-4.html
URL: http://svn.apache.org/viewvc/incubator/stdcxx/trunk/doc/stdlibug/18-4.html?view=diff&rev=544245&r1=544244&r2=544245
==============================================================================
--- incubator/stdcxx/trunk/doc/stdlibug/18-4.html (original)
+++ incubator/stdcxx/trunk/doc/stdlibug/18-4.html Mon Jun 4 13:22:27 2007
@@ -6,54 +6,65 @@
<A HREF="18-3.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="VI.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
<H2>18.4 Example Program: Exceptions</H2>
<BLOCKQUOTE><HR><B>
-NOTE -- This program is in the file exceptn.cpp.
+NOTE -- This program is in the file <code>tutorial/stdexcept.cpp</code>.
</B><HR></BLOCKQUOTE>
<A NAME="idx428"><!></A>
<P>This following example program demonstrates the use of exceptions:</P>
<UL><PRE>
-#include <stdexcept>
-#include <string>
+#include <string> // for string
+#include <stdexcept> // for exception, runtime_error, out_of_range
+#include <iostream> // for cout
-static void f() { throw std::runtime_error("a runtime error"); }
int main ()
{
- std::string s;
-
- // First we'll try to incite then catch an exception
- // from the C++ Standard Library string class.
- // We'll try to replace at a position that is non-existent.
- //
- // By wrapping the body of main in a try-catch block we can be
- // assured that we'll catch all exceptions in the exception
- // hierarchy. You can simply catch an exception as is done
- // below, or you can catch each of the exceptions in which
- // you have an interest.
- try
- {
- s.replace(100,1,1,'c');
- }
- catch (const std::exception& e)
- {
- std::cout << "Got an exception: " << e.what() << std::endl;
- }
+ // First we'll incite and catch an exception from the C++ Standard
+ // library class std::string by attempting to replace a substring
+ // starting at a position beyond the end of the string object.
+ try {
+ std::string ().replace (100, 1, 1, 'c');
+ }
+ catch (std::out_of_range &e) {
- // Now we'll throw our own exception using the function
- // defined above.
- try
- {
- f();
- }
- catch (const std::exception& e)
- {
- std::cout << "Got an exception: " << e.what() << std::endl;
- }
-
- return 0;
+ // Print out the exception string, which in this implementation
+ // includes the location and the name of the function that threw
+ // the exception along with the reason for the exception.
+ std::cout << "Caught an out_of_range exception: "
+ << e.what () << '\n';
+ }
+ catch (std::exception &e) {
+
+ std::cout << "Caught an exception of an unexpected type: "
+ << e.what () << '\n';
+ }
+ catch (...) {
+ std::cout << "Caught an unknown exception\n";
+ }
+
+ // Throw another exception.
+ try {
+ throw std::runtime_error ("a runtime error");
+ }
+ catch (std::runtime_error &e) {
+ std::cout << "Caught a runtime_error exception: "
+ << e.what () << '\n';
+ }
+ catch (std::exception &e) {
+ std::cout << "Caught an exception of an unexpected type: "
+ << e.what () << '\n';
+ }
+ catch (...) {
+ std::cout << "Caught an unknown exception\n";
+ }
+
+ return 0;
}
</PRE></UL>
-
+<P>The exact output of the program is specific to the compiler used to compile it and to the location of the library headers but may look something like this:</P><UL><PRE>
+Caught an out_of_range exception: /usr/local/stdcxx/include/string.cc:422: std::string& std::string::replace(size_type, size_type, size_type, char_type): argument value 100 out of range [0, 0)
+Caught a runtime_error exception: a runtime error
+</UL></PRE>
<BR>
<HR>