📄 chapter28dlg.cpp
字号:
// Chapter28Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Chapter28.h"
#include "Chapter28Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChapter28Dlg dialog
CChapter28Dlg::CChapter28Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CChapter28Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChapter28Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChapter28Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChapter28Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChapter28Dlg, CDialog)
//{{AFX_MSG_MAP(CChapter28Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CATCH_TRY, OnCatchTry)
ON_BN_CLICKED(IDC_ABNORMAL_TER, OnAbnormalTer)
ON_BN_CLICKED(IDC_ABNORMAL_TER2, OnAbnormalTer2)
ON_BN_CLICKED(IDC_DELETE_OBJECT_THROW, OnDeleteObjectThrow)
ON_BN_CLICKED(IDC_DELETE_EXCEPTION, OnDeleteException)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChapter28Dlg message handlers
BOOL CChapter28Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CChapter28Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CChapter28Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CChapter28Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChapter28Dlg::Div_Cus(int numer, int denom)
{
//除法运算中除数为0时将抛出异常
if(denom==0)
{
TRACE("CChapter28Dlg::Div_Cus抛出异常\n");
throw 1;
}
else
{
//除法运算
numer=numer/denom;
TRACE("正常退出CChapter28Dlg::Div_Cus\n");
return;
}
}
void CChapter28Dlg::OnCatchTry()
{
// TODO: Add your control notification handler code here
CString strFile="myvc.dat";
CString strMessage;
TRY
{
//从磁盘文件构造CFile对象,可能抛出异常
CFile file(strFile,CFile::modeRead);
}
CATCH(CFileException, e)
{
switch(e->m_cause)
{
case CFileException::fileNotFound:
{
strMessage.Format("打开文件时发生异常(TRY/CATCH):\
在指定位置没有找到文件%s\n",strFile);
AfxMessageBox(strMessage);
//删除异常
e->Delete();
break;
}
default:
break;
}
}
END_CATCH
}
void CChapter28Dlg::OnAbnormalTer()
{
// TODO: Add your control notification handler code here
CPen* pPen=new CPen();
try
{
//......其他处理过程
Div_Cus(100,0);
}
catch(...)
{
AfxMessageBox("捕获到异常");
}
//退出前销毁堆上创建的对象
TRACE("退出前销毁堆上创建的CPen对象");
delete pPen;
}
void CChapter28Dlg::OnAbnormalTer2()
{
// TODO: Add your control notification handler code here
CPen* pPen=new CPen();
//......其他处理过程
Div_Cus(100,0);
delete pPen;
}
void CChapter28Dlg::OnDeleteObjectThrow()
{
// TODO: Add your control notification handler code here
CBrush* pBrush=new CBrush();
try
{
//......其他处理过程
//此函数调用可能引发异常
Div_Cus(100,0);
}
catch(...)
{
//在继续传递异常前首先删除CBrush对象
TRACE("在继续传递异常前首先删除CBrush对象");
delete pBrush;
throw;
}
//退出前销毁堆上创建的对象
TRACE("退出前销毁堆上创建的CBrush对象");
delete pBrush;
}
void CChapter28Dlg::OnDeleteException()
{
// TODO: Add your control notification handler code here
CString strFile="myvc.dat";
CString strMessage;
try
{
//从磁盘文件构造CFile对象,可能抛出异常
CFile file(strFile,CFile::modeRead);
}
catch(CFileException* e)
{
switch(e->m_cause)
{
case CFileException::fileNotFound:
{
strMessage.Format("打开文件时发生异常(try/catch):\
在指定位置没有找到文件%s\n",strFile);
AfxMessageBox(strMessage);
TRACE("即将删除捕获到的异常对象CFileException::fileNotFound\n");
AfxMessageBox("即将删除捕获到的异常对象CFileException::fileNotFound");
//删除异常对象
e->Delete();
TRACE("成功删除捕获到的异常对象CFileException::fileNotFound\n");
AfxMessageBox("成功删除捕获到的异常对象CFileException::fileNotFound");
break;
}
case CFileException::invalidFile:
{
strMessage.Format("打开文件时发生异常(try/catch):\
文件%无效\n",strFile);
AfxMessageBox(strMessage);
TRACE("即将删除捕获到的异常对象CFileException::invalidFile\n");
AfxMessageBox("即将删除捕获到的异常对象CFileException::invalidFile");
//删除异常对象
e->Delete();
TRACE("成功删除捕获到的异常对象CFileException::invalidFile\n");
AfxMessageBox("成功删除捕获到的异常对象CFileException::invalidFile");
break;
}
default:
break;
}
}
catch(CMemoryException* ee)
{
//其他处理过程
}
catch(CException* eee)
{
//其他处理过程
}
}
/*//查看异常内容,第20章的工程Chapter20中CRightListView类的成员函数,稍作编辑
void CRightListView::AddToList(CSongRecordSet &db,CString& strSql)
{
CListCtrl& listCtrl=GetListCtrl();
listCtrl.DeleteAllItems();
//将数据库中记录插入
try
{
if(db.IsOpen())
db.Close();
//设置查询条件
db.m_strFilter=strSql;
//执行查询
db.Open(CRecordset::snapshot,NULL,CRecordset::none);
…
}
//意外捕获
catch(CDBException* e)
{
//提示用户异常
CString strView;
strView.Format("异常原因:%s\n错误代码:%d",e->m_strError,e->m_nRetCode);
AfxMessageBox(strView);
e->Delete();
//提示异常原因
TRACE("%s",strView);
return;
}
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -