Can anyone tell me how I could get this working please??? I'm trying to create my own exception type which can be used in plase of the .Net framework one:
Code:
Public Class Form1 ...
Private Sub Command1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdGenExcep_DivideBy0.Click
Try
throw new System.OverflowException
Catch Ex as Class1
messagebox.Show(ex.message)
End Try
End Sub
End Class
Public Class Class1
Inherits Exception
Public sub new(ByRef strExcepMessage As String)
mybase.New(strExcepMessage)
End Sub
End Class
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
Public Class HairyException
Inherits System.Exception
Private _mStrException As String
Public sub new(ByRef strExcepMessage As String)
_mStrException = strExcepMessage
End Sub
End Class
Then to use it,
Code:
Private Sub Command1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdGenExcep_DivideBy0.Click
Try
throw new HairyException("OMFG")
Catch Ex as HairyException
messagebox.Show(ex.message)
End Try
End Sub
End Class
One thing I haven't done in the code yet is to create a message property. do this yourself, create a readonly property called message of type string, and use mStrException
Did you try that one out/ did this work on your machine?
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
Public Class HairyException
Inherits System.Exception
Private _msg As String
Public Sub New(ByVal strMsg As String)
_msg = strMsg
End Sub
Overrides ReadOnly Property Message() As String
Get
Return _msg
End Get
End Property
End Class
and in the form:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Throw New HairyException("Holy cow!")
Catch ex As HairyException
MessageBox.Show(ex.Message)
End Try
End Sub
Yes this works fine, if I have a hairyexception thrown manually:
Code:
Public Class frmTesting_ExcepHndl
Inherits System.Windows.Forms.Form ...
Private Sub cmdGenExcep_DivideBy0_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdGenExcep_DivideBy0.Click
Try
throw new HairyException("ddfsdfd") 'System.OverflowException
Catch Ex as HairyException
messagebox.Show(ex.message)
End Try
End Sub
End Class
Public Class HairyException
Inherits System.Exception
Private _mStrException As String
public overrides readonly property message as String
Get
return _mStrException
End Get
End property
Public sub new(ByRef strExcepMessage As String)
_mStrException = strExcepMessage
End Sub
End Class
But....
I want to more or less replace the Exception class - whatever the exception (not just a fat, hairy one). I wanted to more-or-less create a class which deals with the exception (format it into a nice string then print it to the windows event log, that kind of thing).
If I did another exception, say this:
throw new System.OverflowException
Catch Ex as HairyException
The HairyException class won't catch that...
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
Since all exception classes derive from System.Exception, you could use Exception itself and work with its ex.message property.
So in the same code, this should work:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Throw New HairyException("Holy cow!")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
See what I mean?
Now watch as I change its type to another exception thrown
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Throw New System.IO.IOException("omg lololol")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Catch Ex as HairyException
Ex.DoSomethingWithException()
End Try
This bit of code (from above), works fine for a throw Hairy error, but on any other type of error, only catching Ex as a standard .Net exception works.
Catch Ex as HairyException
messagebox.Show(ex.message)
End Try
Am I making any sense here?? I want a class to act exactly as though it were the .Net Exception class, but extended / with some extra functionality/methods added to it too...
Thanks so far btw!!
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
I just wanted to add a few routines to actually do something with the exception:
1) A function to build the exception properties into a nice, friendly error message with all the error info in.
2) Calls to other methods to log the error:
a) to a file
b) to a database
c) to the windows event log
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
So what you want is an event logger. Create a class to which you can pass your error messages to, which will collect the error messages and write to a file or wherever you'd like.
So,
Code:
Try
'something
Catch sqlx as SqlException
EventLog.WriteEntry("An error occured. The error is: " & sqlx.Message)
End Try
This is the kind of thing I'm after thanks (if anyone understands my dodgy attempt at a uml diagram)!!!
Last edited by alex_read; Dec 8th, 2004 at 03:27 AM.
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
clsLogging would be a base class. Beneath it would be classes deriving this & overriding the ExecWriteEntry method - this could be a class which deals with writing to databases, a class which handles text &/or ini files, or (as in this 1st case) a class which deals with the windows event viewer making entries in that...
The clsExceptionThrown would provide a series of the "ExecWriteEntry" methods, with different arguments each time to call this routine in these classes derived from clsLogging.
I.e. I call:
Code:
Catch ex as clsException
ex.ExecWriteEntry(intEventVwrEventId:=999)
This would create an instance of the EventVwr class & make a call to it's ExecWriteEntry method.
I call:
Code:
Catch ex as clsException
ex.ExecWriteEntry(FileNameToWriteOut:="C:\Path\File.txt")
This could create an instance of the TextFile class & make a call to it's ExecWriteEntry method etc.
As a side note:
Those 2 " ExecLogError" labels on the left there should've read "ExecWriteEntry" instead! I've updated the above attachment!
Last edited by alex_read; Dec 8th, 2004 at 03:29 AM.
Please rate this post if it was useful for you!
Please try to search before creating a new post,
Please format code using [ code ][ /code ], and
Post sample code, error details & problem details
mendhak is basically correct with everything.. I see what you are trying to do, but the way the framework works, when an exception is thrown (by your code throwing one, or by the system encountering one) it will look for your catch statements and fire them in order, if you make your own exception class, you HAVE to throw it yourself. This is just how it works in .NET. The point of custom exceptions is to handle custom errors in your custom classes
what you COULD do perhaps is set up global error handling... (I haven't tested but I imagine that any local error handling would override the global handler... but this may be something you can use (the VB.NET code is towards the bottom)
'The Error Handler class
'We need a class because event handling methods can't be static
Friend Class CustomExceptionHandler
'Handle the exception event
Public Sub OnThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs)
Dim result As DialogResult = DialogResult.Cancel
Try
OurExceptionHandler.DisplayMessage(t.Exception)
Catch
Try
MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
Finally
Application.Exit()
End Try
End Try
End Sub
Then hook it in the Main this way:
Public Class Main
'The application's main entry point.
Public Shared Sub Main()
'Explicitly set apartment state to Single Thread Apartment (STA)
'System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA
Dim eh As New CustomExceptionHandler
AddHandler Application.ThreadException, AddressOf eh.OnThreadException
Application.Run(New ErrorHandler)
End Sub
End Class
I hope some of you find this simple, yet elegant, solution as schweet as I did.
this actually made for a good read
Currently using VS 2015 Enterprise on Win10 Enterprise x64.