home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Except_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-08-21  |  3.0 KB  |  87 lines

  1. /*=====================================================================
  2.   File:      Except.cs
  3.  
  4.   Summary:   Demonstrates how to use exceptions
  5.  
  6. ---------------------------------------------------------------------
  7.   This file is part of the Microsoft .NET SDK Code Samples.
  8.  
  9.   Copyright (C) Microsoft Corporation.  All rights reserved.
  10.  
  11. This source code is intended only as a supplement to Microsoft
  12. Development Tools and/or on-line documentation.  See these other
  13. materials for detailed information regarding Microsoft code samples.
  14.  
  15. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  16. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  18. PARTICULAR PURPOSE.
  19. =====================================================================*/
  20.  
  21. using System;
  22. using System.Threading;
  23. using System.Windows.Forms;
  24.  
  25. class App{
  26.    public static void Main(){
  27.       // Setup unhandled exception handler
  28.       Thread.GetDomain().UnhandledException += 
  29.          new UnhandledExceptionEventHandler(HandleUnhandled);
  30.  
  31.       // Function calls that use exception handling blocks to catch exceptions
  32.       ProtectedFunction(1);
  33.       Console.WriteLine();
  34.       ProtectedFunction(2);
  35.       Console.WriteLine();
  36.       ProtectedFunction(3);
  37.       Console.WriteLine();
  38.  
  39.       // This one just throws
  40.       Console.WriteLine(
  41.          "Just about to execute code that throws an unhandled exception:");
  42.       MethodThatThrows(1);
  43.       // This code is never executed
  44.       Console.WriteLine("This code never executes!");
  45.    }
  46.  
  47.    // This method is designed to throw one of three exceptions
  48.    static void MethodThatThrows(int option){
  49.       switch(option){
  50.       case 1:
  51.          int x = 0;
  52.          x = 10 / x;
  53.          break;
  54.       case 2:
  55.          Object o = null;
  56.          Console.WriteLine(o.ToString());
  57.          break;
  58.       case 3:  
  59.          throw(new MyException("This is a custom exception"));
  60.       }
  61.    }
  62.    
  63.    // This method calls the MethodThatThrows, but catches the exceptions
  64.    static void ProtectedFunction(int option){
  65.       try{
  66.          MethodThatThrows(option);
  67.       }catch(Exception e){
  68.          // Dump interesting exception information
  69.          Console.WriteLine ("Exception:\n   {0}", e.GetType().ToString());
  70.          Console.WriteLine ("Message:\n   {0}", e.Message);
  71.          Console.WriteLine ("Stack Trace:\n{0}", e.StackTrace);
  72.          Console.WriteLine ("Help Link:\n   {0}", e.HelpLink);
  73.       }
  74.    }
  75.    
  76.    // Not the most exciting unhandled exception handler in the world
  77.    public static void HandleUnhandled(object sender, UnhandledExceptionEventArgs args){
  78.       MessageBox.Show("Unhandled Exception!\n"+args.ExceptionObject.GetType().ToString(), "Exceptions");         
  79.    }
  80. }
  81.  
  82. // Custom exception class derived from ApplicationException
  83. class MyException:ApplicationException{
  84.    public MyException(String msg):base(msg){
  85.       HelpLink = "http://NotARealURL.Microsoft.com/help.html";
  86.    }
  87. }