30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

466 Exception Handling Chapter 11<br />

12<br />

13 ' construc<strong>to</strong>r for cus<strong>to</strong>mizing error message<br />

14 Public Sub New(ByVal messageValue As String)<br />

15 MyBase.New(messageValue)<br />

16 End Sub ' New<br />

17<br />

18 ' construc<strong>to</strong>r for cus<strong>to</strong>mizing error message and specifying<br />

19 ' InnerException object<br />

20 Public Sub New(ByVal messageValue As String, _<br />

21 ByVal inner As Exception)<br />

22<br />

23 MyBase.New(messageValue, inner)<br />

24 End Sub ' New<br />

25<br />

26 End Class ' NegativeNumberException<br />

Fig. 11.4 ApplicationException derived class thrown when a program<br />

performs an illegal operation on a negative number (part 2 of 2).<br />

1 ' Fig. 11.5: SquareRootTest.vb<br />

2 ' Demonstrating a programmer-defined exception class.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

6 Public Class FrmSquareRoot<br />

7 Inherits Form<br />

8<br />

9 ' Label for showing square root<br />

10 Friend WithEvents lblOutput As Label<br />

11 Friend WithEvents lblInput As Label<br />

12<br />

13 ' But<strong>to</strong>n invokes square-root calculation<br />

14 Friend WithEvents cmdSquareRoot As But<strong>to</strong>n<br />

15<br />

16 ' TextBox receives user's Integer input<br />

17 Friend WithEvents txtInput As TextBox<br />

18<br />

19 ' <strong>Visual</strong> Studio .<strong>NET</strong> generated code<br />

20<br />

21 ' computes square root of parameter; throws<br />

22 ' NegativeNumberException if parameter is negative<br />

23 Public Function SquareRoot(ByVal value As Double) As Double<br />

24<br />

25 ' if negative operand, throw NegativeNumberException<br />

26 If value < 0 Then<br />

27 Throw New NegativeNumberException( _<br />

28 "Square root of negative number not permitted")<br />

29<br />

30 End If<br />

31<br />

Fig. 11.5 FrmSquareRoot class throws an exception if an error occurs when<br />

calculating the square root (part 1 of 2).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!