Demonstrating a user-defined exception class : Custom Exception « Development « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Development » Custom Exception 
7.6.3.Demonstrating a user-defined exception class
Public Class Tester
    Public Shared Sub Main
    
      Try
         Console.WriteLine(SquareRoot(123.123))
         Console.WriteLine(SquareRoot(-123.123))

      Catch formatException As FormatException
         Console.WriteLine(formatException.Message)

      Catch negativeNumberException As _
         NegativeNumberException
         Console.WriteLine(negativeNumberException.Message)
      End Try
    End Sub
   Public Shared Function SquareRoot(ByVal operand As DoubleAs Double

      If operand < Then
         Throw New NegativeNumberException_
            "Square root of negative number not permitted")
      End If

      Return Math.Sqrt(operand)
   End Function ' cmdSquareRoot

End Class


Public Class NegativeNumberException
   Inherits ApplicationException

   Public Sub New()
      MyBase.New("Illegal operation for a negative number")
   End Sub ' New

   Public Sub New(ByVal messageValue As String)
      MyBase.New(messageValue)
   End Sub ' New

   Public Sub New(ByVal messageValue As String, _
      ByVal inner As Exception)
      MyBase.New(messageValue, inner)
   End Sub

End Class
11.0960803890383
Square root of negative number not permitted
7.6.Custom Exception
7.6.1.Create Custom Exception class
7.6.2.Create your own Exception class by subclassing System.Exception
7.6.3.Demonstrating a user-defined exception class
7.6.4.Inherits System.ApplicationException
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.