Errors with my error handlers.

Growler

Member
Joined
Sep 23, 2010
Messages
13
Programming Experience
Beginner
Part of my assignment is to check if input to Group Number and Number of Units are correct input by making error handling exceptions...

I have to check the following:
a) group number is neither 501 nor 062
b) number of units are NOT numbers
c) number of units is NOT a positive number

So my first question is, am I checking correctly?
2nd question is, How do I make sure my Exceptions will pertain to their correct respective things (a, b, and c.. above)?

Thanks!

VB.NET:
        Try
            Dim txtGroupNum(Not "501" Or "062")
            Dim txtUnits As Integer
            Dim txtUnits < 0  

        Catch ex As Exception
            MessageBox.Show("Please enter either Group 501 or 062", "Group number error")
        Catch ex As InvalidCastException
            MessageBox.Show("Please enter a number", "Invalid input!")
        Catch ex As InvalidOperationException
            MessageBox.Show("Please enter a positive number", "Invalid input!")
 
Are you supposed to be catching exceptions or throwing exceptions? It seems to me that the idea is for you to test that the data meets certain criteria and, if it doesn't, throw the appropriate exception for the calling code to catch and handle.
 
Are you supposed to be catching exceptions or throwing exceptions? It seems to me that the idea is for you to test that the data meets certain criteria and, if it doesn't, throw the appropriate exception for the calling code to catch and handle.

Yes, you're correct.
 
Also, since all Exceptions inherit Exception. If you want to catch multiple exceptions you should put them before the "Catch ex As Exception" line, because "Catch ex As Exception" will catch all of them and those listed below it wont ever be reached.
 
If the idea here is to throw exceptions then get rid of the Try...Catch blocks. It's the caller that would Try to execute your method and then Catch the exception(s) that you method Throws. So, you need an If statement that tests for the exceptional conditions you're looking for and, if they're found, then you create an instance of the appropriate Exception-derived class and Throw it. If you're not sure, look up the Throw keyword in the MSDN documentation.
 
Also, since all Exceptions inherit Exception. If you want to catch multiple exceptions you should put them before the "Catch ex As Exception" line, because "Catch ex As Exception" will catch all of them and those listed below it wont ever be reached.

Okay I moved Exception below the other two exceptions. I'm still getting syntax errors from the try block:
VB.NET:
        Try
            Dim txtGroupNum As(Not "501" Or "062")
            Dim txtUnits As Integer
            Dim txtUnits < 0

What this try block is meant to do is check the following:
a) group number is neither 501 nor 062
b) number of units are NOT numbers
c) number of units is NOT a positive number
 
Okay I moved Exception below the other two exceptions. I'm still getting syntax errors from the try block:
VB.NET:
        Try
            Dim txtGroupNum As(Not "501" Or "062")
            Dim txtUnits As Integer
            Dim txtUnits < 0

What this try block is meant to do is check the following:
a) group number is neither 501 nor 062
b) number of units are NOT numbers
c) number of units is NOT a positive number
I explained the exception handling order for you for when you have a situation where you would be catching multiple exceptions. This is not the case, you need to get rid of the try & catches and instead throw exceptions (use the Throw keyword), like jmc has explained several times already.
 
Back
Top