-
Mar 20th, 2014, 02:34 PM
#1
Thread Starter
Member
Handle 'Argument Exception'
Hello guys!
I am trying to find a way to handle the argument exception errors.
I am making a small program with economic functions and I have an issue when I get the ''argument exception'' error.
This is a sample of the code:
Code:
Dim PAYMENT,SPV,R,PERIODS AS DOUBLE
PAYMENT = Val(TextBox1.Text) 'PAYMENT
SPV = Val(TextBox2.Text) 'PRESENT VALUE
R = Val(TextBox3.Text) 'RATE
PERIODS = NPer(R, PAYMENT, SPV, 0)
TextBox5.Text = Format(PERIODS, "STANDARD")
The problem is that if I type random numbers (e.g PAYMENT>SPV or PAYMENT>0 AND SPV>0 ) I get
the argument exception problem because the output is wrong and has no economic meaning.
How can I overcome this problem? I was thinking a checking code like this:
If argument exception ..... then
messagebox .....
exit sub
End if
Is it possible something like this?
Best regards
stratos
-
Mar 20th, 2014, 02:38 PM
#2
Re: Handle 'Argument Exception'
-
Mar 20th, 2014, 03:04 PM
#3
Thread Starter
Member
Re: Handle 'Argument Exception'
OK, Thanks for the support. I gave a couple of tries an I got it how it works.
-
Mar 20th, 2014, 03:32 PM
#4
Re: Handle 'Argument Exception'
It's not the best solution, though. Exception handling costs nothing if no exceptions are raised, but it's fairly slow if there is an exception. Therefore, it is best to avoid exceptions rather than trap them whenever possible. Is it not possible to check your arguments prior to the call so that you can avoid the call if they are invalid?
My usual boring signature: Nothing
-
Mar 20th, 2014, 03:47 PM
#5
Re: Handle 'Argument Exception'
Originally Posted by Shaggy Hiker
It's not the best solution, though. Exception handling costs nothing if no exceptions are raised, but it's fairly slow if there is an exception. Therefore, it is best to avoid exceptions rather than trap them whenever possible. Is it not possible to check your arguments prior to the call so that you can avoid the call if they are invalid?
Agreed. A good example of this would be doing something to the substring:
Code:
Dim fooString As String = originalString.SubString(0, 4)
However if the orginalString's length is 10 then an index out of range error would be thrown a good thing to do would be to use a conditional statement to check that the string's length is greater than or equal to 4 and if so then continue:
Code:
Dim fooString As String = String.Empty
If originalString.Length >= 4 Then
fooString = originalString.SubString(0, 4)
Else
MessageBox.Show("String aint long 'nuff")
End If
That would obviously be quicker than using a Try/Catch and we can let the user know what they did wrong in a more user friendly way.
-
Mar 21st, 2014, 04:23 AM
#6
Lively Member
Re: Handle 'Argument Exception'
Originally Posted by dday9
Agreed. A good example of this would be doing something to the substring:
Code:
Dim fooString As String = originalString.SubString(0, 4)
However if the orginalString's length is 10 then an index out of range error would be thrown a good thing to do would be to use a conditional statement to check that the string's length is greater than or equal to 4 and if so then continue:
Code:
Dim fooString As String = String.Empty
If originalString.Length >= 4 Then
fooString = originalString.SubString(0, 4)
Else
MessageBox.Show("String aint long 'nuff")
End If
That would obviously be quicker than using a Try/Catch and we can let the user know what they did wrong in a more user friendly way.
i don't understand, you said that if "length is 10 then an index out of range error would be thrown" but you still write:
Code:
fooString = originalString.SubString(0, 4)
am i missed something?
-
Mar 21st, 2014, 06:39 AM
#7
Re: Handle 'Argument Exception'
Originally Posted by chipp
am i missed something?
nope... I was going to point that out too
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2014, 08:40 AM
#8
Re: Handle 'Argument Exception'
Yeah, I don't know why I said 10. Pretend I said that it's length is 1
-
Mar 21st, 2014, 12:01 PM
#9
Re: Handle 'Argument Exception'
Well, you just added a zero. Everybody knows that adding 0 to a number doesn't change anything, so it's all the same.
My usual boring signature: Nothing
-
Mar 21st, 2014, 12:08 PM
#10
Re: Handle 'Argument Exception'
Unless you're using binary... then it was really 2, not 10, wait, that's the same thing... ah... so maybe he meant two, not 9+1... unless they're strings, in which case that would be 91... I can see this going in circles...
-tg
-
Mar 21st, 2014, 04:10 PM
#11
Thread Starter
Member
Re: Handle 'Argument Exception'
Originally Posted by Shaggy Hiker
It's not the best solution, though. Exception handling costs nothing if no exceptions are raised, but it's fairly slow if there is an exception. Therefore, it is best to avoid exceptions rather than trap them whenever possible. Is it not possible to check your arguments prior to the call so that you can avoid the call if they are invalid?
I have checked most of them prior to the call, but there are some cases that it is not possible to do anything. In my example (see first post) if you type a small rate (e.g. 3%) and a large present value (e.g. 70.000) will get an error, because output (periods) is out of function range (Nper function). That's a restriction of the function, so I can't change it. These errors are common in economic equations. If user has misunderstood the data of the project or if he hasn't done some transformations before he inserts them to the program, he will eventually get wrong results.
-
Mar 21st, 2014, 04:22 PM
#12
Re: Handle 'Argument Exception'
It sounds like you can predict what inputs will cause exceptions, which might be worth checking for. Of course, if the combinations are too complicated, the only way to really test would be to run the actual equation....which defeats the whole purpose. Just be sure that you are checking for as many conditions as you can before resorting to exception handling. I can often tell when an exception is about to happen because of the pause in the program before the exception pops up. It can be that slow.
My usual boring signature: Nothing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|