home *** CD-ROM | disk | FTP | other *** search
- /*+==========================================================================
- File: client.cpp
-
- Summary: This file implements main(), and instantiates the TestExcept
- and catches exceptions thrown by various methods in the that
- class.
-
- Classes: None
-
- Functions: main, ThrowException, PrintException
-
- ----------------------------------------------------------------------------
- This file is part of the Microsoft NGWS Samples.
-
- Copyright (C) 1998-2000 Microsoft Corporation. All rights reserved.
- ==========================================================================+*/
- #using <mscorlib.dll>
-
- //import the metadata for TestExcept and MyCustomException classes
- #using "MyTestException.dll"
-
- using namespace System;
-
- #define NULL 0
-
- #define THROW_CUSTOMEXCEPTION 0
- #define THROW_DIVIDEBYZEROEXCEPTION 1
- #define THROW_NULLREFERENCEEXCEPTION 3
-
- /*****************************************************************************
- Function: PrintException
-
- Abstract: Outputs a description of the Microsoft::Runtime::Exception that
- was passed into it.
-
- Input Parameters: d (String that describes the exception)
- e (The exception)
-
- Returns: void
- ******************************************************************************/
- void PrintException(String *d, Exception *e)
- {
- Console::WriteLine(String::Concat(d , L": " , e->ToString()));
- Console::WriteLine(String::Concat(L"Message: " , e->Message));
- Console::WriteLine(String::Concat(L"Stack Trace: " , e->StackTrace));
- Console::WriteLine(String::Concat(L"Help Link: " , e->HelpLink));
- //Console::WriteLine(L"ErrorCode: " + Convert::ToString(e->ErrorCode));
- }
-
-
- /*****************************************************************************
- Function: ThrowException
-
- Abstract: Invokes a method on TestExcept which will optionally throw an
- exception and will by caught by this method.
- on the class
-
- Input Parameters: TestExcept (class which will throw exceptions)
- type (the type of exception to throw)
-
- Returns: void
- ******************************************************************************/
- void ThrowException(MyTestException::TestExcept *tst, int type, bool do_throw)
- {
- Console::WriteLine(L"================================"
- L"================================");
- try {
- String *s;
-
- switch(type) {
- case THROW_CUSTOMEXCEPTION:
- s = tst->CustomException(do_throw);
- Console::WriteLine(s);
- break;
-
- case THROW_DIVIDEBYZEROEXCEPTION:
- s = tst->TestDivideByZero(do_throw);
- Console::WriteLine(s);
- break;
-
- case THROW_NULLREFERENCEEXCEPTION:
- s = tst->TestNullReferenceException(do_throw);
- Console::WriteLine(s);
- }
- }
- catch (MyTestException::MyCustomException *e) {
- PrintException(L"MyCustomException",e);
- }
- catch (Exception *e) {
- PrintException(L"Exception",e);
- }
- __finally {
- Console::WriteLine(L"finally called");
- }
-
- }
-
- /*****************************************************************************
- Function: main
-
- Abstract: Creates an instance of the TestExcept class and will invokes its
- various methods to have it throw and let us catch exceptions.
-
- Input Parameters: None
-
- Returns: void
- ******************************************************************************/
- void main()
- {
- //create the new TestExcept object
- Console::WriteLine(L"create the new TestExcept object");
- MyTestException::TestExcept *tst = new MyTestException::TestExcept;
-
- ThrowException(tst, THROW_CUSTOMEXCEPTION, false);
- ThrowException(tst, THROW_DIVIDEBYZEROEXCEPTION, false);
- ThrowException(tst, THROW_NULLREFERENCEEXCEPTION, false);
-
- ThrowException(tst, THROW_CUSTOMEXCEPTION, true);
- ThrowException(tst, THROW_DIVIDEBYZEROEXCEPTION, true);
- ThrowException(tst, THROW_NULLREFERENCEEXCEPTION, true);
-
- Console::WriteLine(L"Done");
- }
-
-