home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / exceptions / VCClient / client.cpp next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.9 KB  |  125 lines

  1. /*+==========================================================================
  2.   File:      client.cpp
  3.  
  4.   Summary:   This file implements main(), and instantiates the TestExcept
  5.              and catches exceptions thrown by various methods in the that
  6.              class.
  7.  
  8.   Classes:   None
  9.  
  10.   Functions: main, ThrowException, PrintException
  11.  
  12. ----------------------------------------------------------------------------
  13.   This file is part of the Microsoft NGWS Samples.
  14.  
  15.   Copyright (C) 1998-2000 Microsoft Corporation.  All rights reserved.
  16. ==========================================================================+*/
  17. #using <mscorlib.dll>
  18.  
  19. //import the metadata for  TestExcept and MyCustomException classes
  20. #using "MyTestException.dll"
  21.  
  22. using namespace System;
  23.  
  24. #define NULL 0
  25.  
  26. #define THROW_CUSTOMEXCEPTION 0
  27. #define THROW_DIVIDEBYZEROEXCEPTION 1
  28. #define THROW_NULLREFERENCEEXCEPTION 3
  29.  
  30. /*****************************************************************************
  31.  Function:  PrintException
  32.  
  33.  Abstract:  Outputs a description of the Microsoft::Runtime::Exception that
  34.             was passed into it.
  35.             
  36.  Input Parameters: d (String that describes the exception)
  37.                    e (The exception)
  38.  
  39.  Returns: void
  40. ******************************************************************************/
  41. void PrintException(String *d, Exception *e)
  42. {
  43.     Console::WriteLine(String::Concat(d , L": " , e->ToString()));
  44.     Console::WriteLine(String::Concat(L"Message: " , e->Message));
  45.     Console::WriteLine(String::Concat(L"Stack Trace: " , e->StackTrace));
  46.     Console::WriteLine(String::Concat(L"Help Link: " , e->HelpLink));
  47.     //Console::WriteLine(L"ErrorCode: " + Convert::ToString(e->ErrorCode));
  48. }
  49.  
  50.  
  51. /*****************************************************************************
  52.  Function:  ThrowException
  53.  
  54.  Abstract:  Invokes a method on TestExcept which will optionally throw an
  55.             exception and will by caught by this method.
  56.             on the class
  57.             
  58.  Input Parameters: TestExcept (class which will throw exceptions)
  59.                    type (the type of exception to throw)
  60.  
  61.  Returns: void
  62. ******************************************************************************/
  63. void ThrowException(MyTestException::TestExcept *tst, int type, bool do_throw)
  64. {
  65.     Console::WriteLine(L"================================"
  66.             L"================================");
  67.     try {
  68.         String *s;
  69.  
  70.         switch(type) {
  71.         case THROW_CUSTOMEXCEPTION:
  72.         s = tst->CustomException(do_throw);
  73.         Console::WriteLine(s);
  74.         break;
  75.  
  76.         case THROW_DIVIDEBYZEROEXCEPTION:
  77.         s = tst->TestDivideByZero(do_throw);
  78.         Console::WriteLine(s);
  79.         break;
  80.  
  81.         case THROW_NULLREFERENCEEXCEPTION:
  82.         s = tst->TestNullReferenceException(do_throw);
  83.         Console::WriteLine(s);
  84.         }
  85.     }
  86.     catch (MyTestException::MyCustomException *e) {
  87.         PrintException(L"MyCustomException",e);
  88.     }
  89.     catch (Exception *e) {
  90.         PrintException(L"Exception",e);
  91.     }
  92.     __finally {
  93.         Console::WriteLine(L"finally called");
  94.     }
  95.  
  96. }
  97.  
  98. /*****************************************************************************
  99.  Function: main
  100.  
  101.  Abstract: Creates an instance of the TestExcept class and will invokes its
  102.            various methods to have it throw and let us catch exceptions.
  103.             
  104.  Input Parameters: None
  105.  
  106.  Returns: void
  107. ******************************************************************************/
  108. void main()
  109. {
  110.     //create the new TestExcept object
  111.     Console::WriteLine(L"create the new TestExcept object");
  112.     MyTestException::TestExcept *tst = new MyTestException::TestExcept;
  113.  
  114.     ThrowException(tst, THROW_CUSTOMEXCEPTION, false);
  115.     ThrowException(tst, THROW_DIVIDEBYZEROEXCEPTION, false);
  116.     ThrowException(tst, THROW_NULLREFERENCEEXCEPTION, false);
  117.  
  118.     ThrowException(tst, THROW_CUSTOMEXCEPTION, true);
  119.     ThrowException(tst, THROW_DIVIDEBYZEROEXCEPTION, true);
  120.     ThrowException(tst, THROW_NULLREFERENCEEXCEPTION, true);
  121.  
  122.     Console::WriteLine(L"Done");
  123. }
  124.  
  125.