VB.Net/Development/Exception Stack — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Display Exception Stack Trace

Imports System
Public Class MainClass
    
    Shared Sub Main(ByVal args As String())
        Dim stack_trace As New System.Diagnostics.StackTrace(True)
        For i As Integer = 0 To stack_trace.FrameCount - 1
            With stack_trace.GetFrame(i)
                Console.WriteLine("Method: " & .GetMethod().ToString & _
                ", File: "" & _
                .GetFileName() & _
                "", Line: " & .GetFileLineNumber() )
            End With
        Next i
    End Sub
End Class


Inner Exception Demo

Imports System
Imports System.Text
Imports System.Text.RegularExpressions

Public Class MainClass
         Shared Sub Main(  )
             Try
                 DangerousFunc1(  )
             Catch e As MyCustomException
                 Console.WriteLine(ControlChars.Lf + "{0}", e.Message)
                 Console.WriteLine("Retrieving exception history...")
                 Dim inner As Exception = e.InnerException
                 While Not (inner Is Nothing)
                     Console.WriteLine("{0}", inner.Message)
                     inner = inner.InnerException
                 End While
             End Try
         End Sub "Main
         Shared Public Sub DangerousFunc1(  )
             Try
                 DangerousFunc2(  )
             Catch e As System.Exception
                 Dim ex As New MyCustomException( _
                  "E3 - Custom Exception Situation!", e)
                 Throw ex
             End Try
         End Sub
         Shared Public Sub DangerousFunc2(  )
             Try
                 DangerousFunc3(  )
             Catch e As System.DivideByZeroException
                 Dim ex As New Exception( _
                     "E2 - Func2 caught divide by zero", e)
                 Throw ex
             End Try
         End Sub "DangerousFunc2
         Shared Public Sub DangerousFunc3(  )
             Try
                 DangerousFunc4(  )
             Catch e As System.ArithmeticException
                 Throw e
             Catch e As System.Exception
                 Console.WriteLine("Exception handled here!")
             End Try
         End Sub "DangerousFunc3
         Shared Public Sub DangerousFunc4(  )
             Throw New DivideByZeroException("E1 - DivideByZero Exception")
         End Sub
   
End Class
     Public Class MyCustomException
         Inherits System.ApplicationException
         Public Sub New(ByVal message As String, ByVal inner As Exception)
             MyBase.New(message, inner)
         End Sub "New
     End Class "MyCustomException