Imports System.IO

Public Class ExceptionTester

    Private Sub cmdThrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThrow.Click
        Dim FileLine As String
        Dim FileStream As System.IO.StreamReader


        Try
            ' This code could cause a problem . . .
            FileStream = System.IO.File.OpenText("does_not_exist.txt")
            FileLine = FileStream.ReadLine()

        Catch MyError As Exception
            ' We end up here if an error occurred.

            DisplayError(MyError)

        Finally
            ' We end up here no matter what!
            If Not FileStream Is Nothing Then
                FileStream.Close()   ' Close the file.
            End If
        End Try

    End Sub


    Private Sub cmdThrowCustom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThrowCustom.Click

        ' The invalid data.
        Dim IsPetOwner As Boolean = False
        Dim PetBreed As String = "Daschund"

        Try
            Dim MyError As New ConflictingDBDataException("A pet problem.")
            MyError.InvalidFields.Add("IsPetOwner", IsPetOwner)
            MyError.InvalidFields.Add("PetBreed", PetBreed)
            Throw MyError
        Catch MyError As ConflictingDBDataException
            DisplayError(MyError)
            MessageBox.Show("Number of invalid data pieces: " & MyError.InvalidFields.Count)
        End Try

    End Sub

    Private Sub DisplayError(ByVal Err As Exception)
        Dim Spacer As New String("-", 150)
        Spacer = vbNewLine & Spacer & vbNewLine

        Dim Message As String
        Message = "Exception Type" & Spacer
        Message &= Err.GetType().ToString() & vbNewLine & vbNewLine
        Message &= "Message" & Spacer
        Message &= Err.Message & vbNewLine & vbNewLine
        Message &= "Stack Trace" & Spacer
        Message &= Err.StackTrace

        MessageBox.Show(Message, "Exception Occurred")

    End Sub

    Private Sub cmdFail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFail.Click
        Dim Balance As Integer = 0

        ' This is false, which means the assertion will fail.
        Debug.Assert(Balance > 0)
    End Sub

    Private Sub cmdDebug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDebug.Click

        Dim i As Integer
        Dim Info() As String = {"This", "is", "just", "a", "test", "of", "the", "debug", "output", "window.", ""}
        For i = 0 To 10
            Debug.WriteLine("Iteration number " & i.ToString & ". Read: " & Info(i))
        Next i
    End Sub

    Private Sub cmdTrace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTrace.Click

        ' Clean up.
        Trace.Listeners.Clear()
        If File.Exists("c:\output.text") Then
            File.Delete("c:\output.txt")
        End If

        ' Set up.
        Dim FileListener As New TextWriterTraceListener(File.Create("c:\output.txt"))
        Trace.Listeners.Add(FileListener)
        Trace.AutoFlush = True

        ' Trace.
        Trace.WriteLine("Starting application")
        Trace.Indent() ' This is a nice touch to make well organized output.
        Trace.WriteLine("Doing some task . . .")
        ' Add some code here.
        Trace.WriteLine("Finished task")
        Trace.Unindent()
        Trace.WriteLine("All done")

        ' This is usually performed at the end of the program.
        Trace.Listeners(0).Close()

        MessageBox.Show("Log created in c:\output.txt.")

    End Sub

    Private Sub cmdThrowUnhandled_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThrowUnhandled.Click
        Throw New ApplicationException( _
         "This will shut down your application unless you handle " & _
         "the UnhandledException application event.")
    End Sub
End Class

