?? exa_1053.htm
字號:
<HTML><HEAD><TITLE>18.4 Example Program</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="usi_0332.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="aut_3512.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>18.4 Example Program</H2><A HREF="sidebar1.htm#sidebar76"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Obtaining the Sample Program.</STRONG></A><P>This following example program demonstrates the use of exceptions.</P><PRE>#include <stdexcept>#include <string>static void f() { throw runtime_error("a runtime error"); }int main (){ string s; // First we'll try to incite then catch an exception from // the 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 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 exception& e) { cout << "Got an exception: " << e.what() << endl; } // Now we'll throw our own exception using the function // defined above. try { f(); } catch (const exception& e) { cout << "Got an exception: " << e.what() << endl; } return 0;}</PRE><BR><HR><A HREF="usi_0332.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="aut_3512.htm"><IMG SRC="images/next.gif"></A><P>©Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -