OwnExcept.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:1k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- #include <tchar.h>
- using namespace System;
- // User-defined exception class
- __gc class MyException : public System::Exception
- {
- public:
- int errNo;
- MyException(String* msg, int num) : Exception(msg), errNo(num) {}
- };
- void func(int a)
- {
- try
- {
- if (a <= 0)
- throw new System::ArgumentException("Negative argument");
- }
- catch(System::ArgumentException* pex)
- {
- Console::WriteLine(S"Caught ArgumentException in func()");
- throw new MyException(pex->get_Message(), 1000);
- }
- }
- int _tmain(void)
- {
- // TODO: Please replace the sample code below with your own.
- Console::WriteLine(S"Custom Exceptions");
- try
- {
- func(0);
- }
- catch(MyException* pex)
- {
- Console::WriteLine(S"Caught MyException in main()");
- Console::WriteLine(S"Message is : {0}", pex->get_Message());
- Console::WriteLine(S"Error number is : {0}", __box(pex->errNo));
- }
- return 0;
- }