Results 1 to 12 of 12

Thread: Handle 'Argument Exception'

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    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

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Handle 'Argument Exception'

    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Handle 'Argument Exception'

    OK, Thanks for the support. I gave a couple of tries an I got it how it works.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Handle 'Argument Exception'

    Quote Originally Posted by Shaggy Hiker View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Re: Handle 'Argument Exception'

    Quote Originally Posted by dday9 View Post
    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?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Handle 'Argument Exception'

    Quote Originally Posted by chipp View Post
    am i missed something?
    nope... I was going to point that out too

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Handle 'Argument Exception'

    Yeah, I don't know why I said 10. Pretend I said that it's length is 1
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Handle 'Argument Exception'

    Quote Originally Posted by Shaggy Hiker View Post
    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.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    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
  •  



Click Here to Expand Forum to Full Width