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

  1. '=====================================================================
  2. '  File:      Except.vb
  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. Option Explicit On 
  22. Option Strict On
  23.  
  24.  
  25. Imports System
  26. Imports System.Threading
  27. Imports System.Windows.Forms
  28. Imports Microsoft.VisualBasic
  29.  
  30. Public Class App
  31.    Public Shared Sub Main()
  32.       'Setup unhandled exception handler
  33.       AddHandler Thread.GetDomain().UnhandledException, AddressOf App.HandleUnhandled
  34.  
  35.       'Function calls that use exception handling blocks to catch exceptions
  36.       ProtectedFunction(1)
  37.       Console.WriteLine()
  38.       ProtectedFunction(2)
  39.       Console.WriteLine()
  40.       ProtectedFunction(3)
  41.       Console.WriteLine()
  42.  
  43.       'This one just throws
  44.       Console.WriteLine("Just about to execute code that throws an unhandled exception:")
  45.       MethodThatThrows(1)
  46.       'This code is never executed
  47.       Console.WriteLine("This code never executes!")
  48.    End Sub
  49.  
  50.    'This method is designed to throw one of three exceptions
  51.    Public Shared Sub MethodThatThrows(opt As Integer)
  52.       Select Case opt
  53.       Case 1
  54.          Dim x As Byte
  55.          x = 230
  56.          x = x + x
  57.       Case 2
  58.          Dim o As Object          
  59.          Console.WriteLine(o.ToString())
  60.       Case 3
  61.          throw(new MyException("This is a custom exception"))
  62.       End Select
  63.    End Sub
  64.  
  65.    'This method calls the MethodThatThrows, but catches the exceptions   
  66.    Public Shared Sub ProtectedFunction(opt As Integer)
  67.       Try
  68.          MethodThatThrows(opt)
  69.       Catch e As Exception
  70.          Console.WriteLine ("Exception:" & ControlChars.CrLf & "   " & e.GetType().ToString())
  71.          Console.WriteLine ("Message:" & ControlChars.CrLf & "   " & e.Message)
  72.          Console.WriteLine ("Stack Trace:" & ControlChars.CrLf & e.StackTrace)
  73.          Console.WriteLine ("Help Link:" & ControlChars.CrLf & "   " & e.HelpLink)
  74.       End Try
  75.    End Sub
  76.  
  77.    'Not the most exciting unhandled exception handler in the world
  78.    Public Shared Sub HandleUnhandled(sender As Object, args As UnhandledExceptionEventArgs)
  79.         MessageBox.Show("Unhandled Exception!" & ControlChars.CrLf & args.ExceptionObject.GetType().ToString(), "Exceptions")
  80.    End Sub
  81.  
  82. End Class
  83.  
  84. 'Custom exception class derived from ApplicationException
  85. Class MyException 
  86. Inherits ApplicationException
  87.    Public Sub New(msg As String)
  88.       MyBase.New(msg)
  89.       HelpLink = "http://NotARealURL.Microsoft.com/help.html"
  90.    End Sub
  91. End Class
  92.  
  93.  
  94.  
  95.  
  96.  
  97.