6 Replies - 5617 Views - Last Post: 29 September 2009 - 07:55 PM Rate Topic: -----

#1 miketeebo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-August 09

User-Defined Exception Class

Post icon  Posted 29 September 2009 - 06:14 PM

I am currently working on my Final Project and I need to incorporate some Exception Handling. I have created one in the following code but it isn't working. I don't have any "Errors" or "Warnings" but when I run the program it says the "BadNameException was unhandled." I thought I followed the steps correctly from my book, but I am stuck somewhere.

Anyone have any ideas?

As always thanks for your help.


'Exception Class
Public Class BadNameException
	Inherits Exception
	Public Sub New()
		MyBase.New("Bad Name Format")
	End Sub
	Public Sub New(ByVal messageValue As String)
		MyBase.new(messageValue)
	End Sub

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


Public Class frmPTCalculator

	Dim Age2 As String
	Dim Age3 As Integer

	Private Sub txtAge_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAge.TextChanged
		Age2 = txtAge.Text
	End Sub

	Public Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

		' check to make sure the date is in correct format
		If Not IsDate(txtDate.Text) Then
			MsgBox("Please enter a date in the following format: mm/dd/year", MsgBoxStyle.OkOnly, "Invalid Date")
		Else
			date2 = DateValue(txtDate.Text)
		End If

		' check to make sure a Last name was entered
		If txtLast.Text = "" Then
			Throw New BadNameException("Please Enter a Last Name")
		Else
			LName = txtLast.Text
		End If

		' check to make sure a First name was entered
		If txtFirst.Text = "" Then
			Throw New BadNameException("Please Enter a First Name")
		Else
			FName = txtFirst.Text
		End If

		'check age
		Age3 = Convert.ToInt32(Age2)
		If txtAge.Text = "" Then
			MsgBox("Please enter an Age", MsgBoxStyle.OkOnly, "Invalid Age")
		ElseIf Age3 < 99 Then
			MsgBox("Please enter an Age below 100", MsgBoxStyle.OkOnly, "Invalid Age")
		Else
			Age = txtAge.Text
		End If

		' decide if male or female was checked
		If rbtnMale.Checked Then
			Male.Show()
		ElseIf rbtnFemale.Checked Then
			Female.Show()
		End If

		' catch the BadNameException
		Try
		Catch BadNameExceptionParameter As BadNameException
			MessageBox.Show(BadNameExceptionParameter.Message, "Invalid Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try

	End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: User-Defined Exception Class

#2 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: User-Defined Exception Class

Posted 29 September 2009 - 06:25 PM

It says the exception was unhandled because it was! Your program successfully threw the 'BadNameException' and you are getting the error because you didn't try to handle the exception (using, say, a try-catch block).

What sort of result are expecting from this?
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: User-Defined Exception Class

Posted 29 September 2009 - 06:38 PM

As already mentioned, you have to use a Try...Catch block to set a certain message to be displayed. If you just throw it like that it's going to tell you an unhandled exception occurred. You can read up on the Try...Catch Block at that link :)
Was This Post Helpful? 0
  • +
  • -

#4 miketeebo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-August 09

Re: User-Defined Exception Class

Posted 29 September 2009 - 07:04 PM

Thanks for the reply. At least I know it is somewhat doing what it is suppose to. I have this towards the end of the code.

		Try
		Catch BadNameExceptionParameter As BadNameException
			MessageBox.Show(BadNameExceptionParameter.Message, "Invalid Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try



I thought that would "catch" it, but I must have something wrong?
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: User-Defined Exception Class

Posted 29 September 2009 - 07:14 PM

You have to do your processing inside the Try part, then the Catch catches any exceptions that happen or that you throw. Did you even take a look at the link I provided?
Was This Post Helpful? 0
  • +
  • -

#6 miketeebo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 12-August 09

Re: User-Defined Exception Class

Posted 29 September 2009 - 07:48 PM

PsychoCoder, trust me I look at any suggestions I get! I just figured that if I didn't have anything after the "Try" part then it would go straight to the "Catch" part.

So the way it is looking right now I need to put something in after "Try."

......


OK, I looked at a few more examples and this is what I got. It seems to work. What do you think, is this the proper way for what I am wanting/doing?

		Try
			' check to make sure a Last name was entered
			If txtLast.Text = "" Then
				Throw New BadNameException("Please Enter a Last Name")
				' MsgBox("Please enter a Last Name", MsgBoxStyle.OkOnly, "Invalid Name")
			Else
				LName = txtLast.Text
			End If
		Catch BadNameExceptionParameter As BadNameException
			MessageBox.Show(BadNameExceptionParameter.Message, "Invalid Name", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try


Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: User-Defined Exception Class

Posted 29 September 2009 - 07:55 PM

That's exactly how a Try...Catch block is used :) Good job :^:
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1