稍微明白try catch的用处,但谁能给我讲讲try catch在程序中的重要性?
举个例子最好的说,呵呵~

解决方案 »

  1.   

    msdn上有很多的 自己看看吧!没有的话 我给你贴上来!
      

  2.   

    try
    {
       errror
    }
    catch()
    {
         writelog()
         reminduser()
    }可以用来跟踪错误写出日志,也可以提醒用户操作非法等等 很多的
      

  3.   

    The try, catch, and throw StatementsSee Also
    C++ Exception Handling | C++ Keywords | Function-try Blocks | Unhandled C++ Exceptions
    Grammar try-block : 
    try compound-statement handler-list 
    handler-list : 
    handler handler-listopt 
    handler : 
    catch ( exception-declaration ) compound-statement 
    exception-declaration : 
    type-specifier-list declarator
    type-specifier-list abstract-declarator
    type-specifier-list
    ... 
    throw-expression : 
    throw assignment-expressionopt 
    The C++ language provides built-in support for handling anomalous situations, known as exceptions, which may occur during the execution of your program. The try, throw, and catch statements implement exception handling. With C++ exception handling, your program can communicate unexpected events to a higher execution context that is better able to recover from such abnormal events. These exceptions are handled by code which is outside the normal flow of control. The Microsoft C++ compiler implements the C++ exception handling model based on the ANSI C++ standard.Visual C++ also supports throw(...) syntax. throw(...) tells the compiler that a function could throw an exception. This is useful if you want your functions to explicitly specify whether or not they will throw exceptions. For example:void MyFunc()  throw(...) {
       throw 1;
    }
    Note   The Win32 structured exception-handling mechanism works with both C and C++ source files. However, it is not specifically designed for C++. You can ensure that your code is more portable by using C++ exception handling. Also, C++ exception handling is more flexible, in that it can handle exceptions of any type. For C++ programs, it is recommended that you use the C++ exception-handling mechanism (try, catch, throw) described in this topic.
    The compound-statement after the try clause is the guarded section of code. The throw-expression throws (raises) an exception. The compound-statement 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 C++ 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 be the last handler for its try block.The operand of throw is syntactically similar to the operand of a return statement.Execution proceeds as follows: Control reaches the try statement by normal sequential execution. The guarded section within the try block is executed. 
    If no exception is thrown during execution of the guarded section, the catch clauses that follow the try block are not executed. Execution continues at the statement after the last catch clause following the try block in which the exception was thrown. 
    If an exception is thrown during execution of the guarded section or in any routine the guarded section calls (either directly or indirectly), an exception object is created from the object created by the throw operand. (This implies that a copy constructor may be involved.) At this point, the compiler looks for a catch clause in a higher execution context that can handle an exception of the type thrown (or a catch handler that can handle any type of exception). The catch handlers are examined in order of their appearance following the try block. If no appropriate handler is found, the next dynamically enclosing try block is examined. This process continues until the outermost enclosing try block is examined. 
    If a matching handler is still not found, or if an exception occurs while unwinding, but before the handler gets control, the predefined run-time function terminate is called. If an exception occurs after throwing the exception, but before the unwind begins, terminate is called. 
    If a matching catch handler is found, and it catches by value, its formal parameter is initialized by copying the exception object. If it catches by reference, the parameter is initialized to refer to the exception object. After the formal parameter is initialized, the process of unwinding the stack begins. This involves the destruction of all automatic objects that were constructed (but not yet destructed) between the beginning of the try block associated with the catch handler and the exception's throw site. Destruction occurs in reverse order of construction. The catch handler is executed and the program resumes execution following the last handler (that is, the first statement or construct which is not a catch handler). Control can only enter a catch handler through a thrown exception, never via a goto statement or a case label in a switch statement. 
    The following is a simple example of a try block and its associated catch handler. This example detects failure of a memory allocation operation using the new operator. If new is successful, the catch handler is never executed:
      

  4.   

    // exceptions_trycatchandthrowstatements.cpp 
    // compile with: /EHsc
    #include <iostream>using namespace std;
    int main()
    {
        char *buf;
        try
        {
            buf = new char[512];
            if( buf == 0 )
                throw "Memory allocation failure!";
        }
        catch( char * str )
        {
            cout << "Exception raised: " << str << '\n';
        }
        // ...
        return 0;
    }
    The operand of the throw expression specifies that an exception of type char * is being thrown. It is handled by a catch handler that expresses the ability to catch an exception of type char *. In the event of a memory allocation failure, this is the output from the preceding example:
    Exception raised: Memory allocation failure!
    The real power of C++ exception handling lies not only in its ability to deal with exceptions of varying types, but also in its ability to automatically call destructor functions during stack unwinding, for all local objects constructed before the exception was thrown.
    The following example demonstrates C++ exception handling using classes with destructor semantics:
    Example
    // exceptions_trycatchandthrowstatements2.cpp 
    // compile with: /EHsc
    #include <iostream>
    using namespace std;
    void MyFunc( void );class CTest {
    public:
       CTest(){};
       ~CTest(){};
       const char *ShowReason() const { return "Exception in CTest class."; }
    };class CDtorDemo {
    public:
       CDtorDemo();
       ~CDtorDemo();
    };CDtorDemo::CDtorDemo() {
       cout << "Constructing CDtorDemo.\n";
    }CDtorDemo::~CDtorDemo() {
       cout << "Destructing CDtorDemo.\n";
    }void MyFunc() {
       CDtorDemo D;
       cout<< "In MyFunc(). Throwing CTest exception.\n";
       throw CTest();
    }int main() {
       cout << "In main.\n";
       try {
           cout << "In try block, calling MyFunc().\n";
           MyFunc();
       }
       catch( CTest E ) {
           cout << "In catch handler.\n";
           cout << "Caught CTest exception type: ";
           cout << E.ShowReason() << "\n";
       }
       catch( char *str )    {
           cout << "Caught some other exception: " << str << "\n";
       }
       cout << "Back in main. Execution resumes here.\n";
       return 0;
    }
    Output
    In main.
    In try block, calling MyFunc().
    Constructing CDtorDemo.
    In MyFunc(). Throwing CTest exception.
    Destructing CDtorDemo.
    In catch handler.
    Caught CTest exception type: Exception in CTest class.
    Back in main. Execution resumes here.
    Note that in this example, the exception parameter (the argument to the catch clause) is declared in both catch handlers:
    catch( CTest E )
    // ...
    catch( char *str )
    // ...
    You do not need to declare this parameter; in many cases it may be sufficient to notify the handler that a particular type of exception has occurred. However, if you do not declare an exception object in the exception-declaration, you will not have access to that object in the catch handler clause.
    A throw-expression with no operand re-throws the exception currently being handled. Such an expression should appear only in a catch handler or in a function called from within a catch handler. The re-thrown exception object is the original exception object (not a copy). For example:
    try {
        throw CSomeOtherException();
    }
    catch(...) {  // Handle all exceptions
        // Respond (perhaps only partially) to exception
        // ...
        throw;       // Pass exception to some other handler
    }
    An empty throw statement tells the compiler that the function does not throw any exceptions. It is the equivalent to using __declspec(nothrow). For example:
    // exceptions_trycatchandthrowstatements3.cpp 
    void empty() throw()
    {
       puts("In empty()");
    }void with_type() throw(int)
    {
       puts("Will throw an int");
       throw(1);
    }int main()
    {
       try {
           empty();
           with_type();
       }catch (int){
           puts("Caught an int");
       }
    }
      

  5.   

    catch对应参数的异常的捕捉有一些规定,建议找本C++的书看一看
      

  6.   

    捕获到异常以后可以防止程序down掉,而且你可以知道出错的原因,非常好用的
      

  7.   

    一个定时器不断的读写,关闭一个文件,当文件被人删了怎么办?再去找那个文件当然没有了,不用try catch程序就“哐”弹出一个对话框,然后用户按确定又得重新启动程序,用了try程序不仅不会退出,而且还能知道是什么原因得异常。
    读写文件可以catch(CFileException *pe),读写数据库可以用catch(CDataBase *pe),
    类似其他
    简而言之:不让你得程序出现异常得时候死掉,捕获程序中出现得错误
      

  8.   

    当程序语句里面有严重错误是if(){}没用,系统会直接报错。
    try{}catch(){}可以防止报这样的错误。
    避免尴尬场面呵呵
      

  9.   

    认为有可能出错的地方,就try一下,如果真的出错,那就会catch了
      

  10.   

    我也是初学者,最好是把catch对应参数的异常中是那些参数写出来