excep2.cpp

来自「vc++算法程序员处理算法经典 还可以继承其他程序使用」· C++ 代码 · 共 69 行

CPP
69
字号
#include    <windows.h>
#include    <stdio.h>
#include    <iostream.h>
//
//  excep2.cpp. Test program for exception handling.
//  compile this program with the following command line
//
//            cl -GX excep2.cpp user32.lib
//
//  The -GX enables exception handling. The user32.lib provides
//  the MessageBox function.
//
void TestFileFunc ();
void TestCharFunc ();
void TestIntFunc ();

int g_Var1 = 0;
int g_Var2 = 0;
int g_Var3 = 0;

main ()
{
    try
    {
        TestCharFunc ();
    }
    catch (char *ErrorMsg)
    {
        MessageBox (NULL, ErrorMsg, "Howdy", MB_OK);
    }
    try
    {
        TestIntFunc ();
    }
    catch (int x)
    {
        MessageBox (NULL, "Caught int exception", "Howdy", MB_OK);
    }
    try
    {
        TestFileFunc ();
    }
    catch (long lx)
    {
        MessageBox (NULL, "Caught long exception", "Howdy", MB_OK);
    }
    cout << "g_Var1 = " << g_Var1 << endl;
    cout << "g_Var2 = " << g_Var2 << endl;
    cout << "g_Var3 = " << g_Var3 << endl;
}

void TestIntFunc()
{
    g_Var1 = 10;
    throw (-1);
}

void TestCharFunc()
{
    g_Var2 = 20;
    throw ("Error condition message");
}

void TestFileFunc()
{
    g_Var3 = 30;
    throw (-1L);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?