Page 1 of 1

Custom Exceptions Creating your own Exception. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 01 April 2010 - 12:35 PM

Custom Exceptions

Often when I create my own type classes and I need to throw an exception but the one available in the .net framework ain't specific enough. So solve this I create my own exception.
Creating your own exception couldn't be any simpler.

Namespace Exceptions
 Public Class ExampleException
  Inherits Exception
  Public Sub New()
   MyBase.New()
  End Sub
  Public Sub New(ByVal message As String)
   MyBase.New(message)
  End Sub
  Public Sub New(ByVal message As String, ByVal InnerExecption As Exception)
   MyBase.New(message, InnerExecption)
  End Sub
  Public Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
   MyBase.New(info, context)
  End Sub
 End Class
End Namespace


Review
Create you own class, inherit from Exception and the create the 4 overloaded New subs.

Throwing an Exception
Just like normal exceptions custom exception are thrown in same way.
IF result is some error Throw New ExampleException



Catching an Exception
And also catch in the same way.
Try 

Catch ex As ExampleException
' Catches just Example Exceptions '
Catch ex As Exception
' Catches all other execptions '




Inherited Exceptions

It is also possible to create a family of exceptions.
Create an exception that I act as a base of the exceptions
For example Bar_Exception
The create other exceptions derived from that type.
Public Class SplitMyPint
 Inherits Bar_Exception
' code snipped '
End Class

Public Class YouLookingAtMyBird
 Inherits Bar_Exception
' code snipped '
End Class



Is This A Good Question/Topic? 1
  • +

Replies To: Custom Exceptions

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 23 May 2013 - 12:16 PM

VS Item Templates
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Posted 23 May 2013 - 02:34 PM

Spilling my pint needs to be higher in the class hierarchy than looking at my bird :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1