Throw statement

M

marcmc

I cannot get my throw statement to run. The program hits
the 1 and 3 message boxes but not 2. I have included
Imports System.Exception
Imports Microsoft.VisualBasic

Private Sub btnLogError_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnLogError.Click
Dim at As New ErrorClassTest
Try
MessageBox.Show("1")
Throw New System.Exception("An error has
occurred")
MessageBox.Show("2")
Catch ex As Exception
MessageBox.Show("3")
If Not (at.LogError("\My Documents\Exception
Handler.log", _
ex.Message.ToString, _
"btnLogError_Click", _
"Testing Log File....")) Then
MessageBox.Show("Could not write to the
Log File", MessageBoxIcon.Exclamation)
End If
End Try
End Sub
 
W

William Ryan

Once you throw your System.Exception, execution shifts to the handler which
in turn causes 3 to show.
 
M

marcmc

Thats fair enough but the throw statement is not
executing, something should be thrown to the screed,
correct?

I also have an ErrorHandlerClass which should right this
exception to a log file when it occurs but obviously does
not because its not executed.
I got this out of a book and copied it meticulously and
it still wont work. Its 1 screen, 1 button, 1 class and a
click event on the button. Very simple you would think!!
Any further ideas?
 
C

Chris Tacke, eMVP

If you're seeing 1 and 3, then the throw is executing - it's then getting
caught by the try/catch block and properly moving into the catch.
 
M

marcmc

fair enough again but nothing is happening in my log file.

my class is as follows, am i missing something very
simple?

Imports Microsoft.VisualBasic
Imports System.Exception
Public Class ErrorClassTest
Function LogError(ByVal LogFile As String, ByVal
Message As String, _
ByVal Source As String, ByVal Comments As String)
As Boolean

Dim strLogRecord As String
Dim sw As System.IO.StreamWriter

Try
' Open the log()
sw = New System.IO.StreamWriter(LogFile, True)

' Add item to the log
strLogRecord = Now.ToString & "|" & Message
& "|" & Source & _
"|" & Comments & "|"
sw.WriteLine(strLogRecord)

'Close the log
sw.Close()

'Return a positive result
LogError = True

' A runtime error was generated. Return a
negative result.
Catch
LogError = False
End Try
End Function
End Class
 
G

Ginny Caughey [MVP]

marcmc,

What is the value of LogError?If it is false, an error occurred when you
were trying to write to the log file and the try...catch block caught it for
you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top