CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2004
    Posts
    37

    Question How to check Exceptions while running

    Hi , i wonder how could i check, lets say that there will be
    ArrayIndexOutOfBoundsException when trying indexing my array with too big
    index value.

    Is it possible to check that if there's ArrayIndexOutOfBoundsException , application continues running without showing that exception but diminishing
    indexing value as long as its needed to get out of exception situation.

    Thanks.

  2. #2
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: How to check Exceptions while running

    A RuntimeException like ArrayIndexOutOfBoundsException generally means your code is flawed and has a bug.

    Every array has a length field that you can use to check the index against:
    Code:
    int i = ...;
    int[] arr = ....;
    if (i >= arr.length) // index out of bounds
    However, without knowing the context of your code, we can't really provide a good solution.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  3. #3
    Join Date
    Jul 2005
    Location
    Ontario, Canada
    Posts
    107

    Re: How to check Exceptions while running

    Yes, your code is usually flawed if it is possible to get an ArrayIndexOutOfBoundsException. However, if you want to be able to handle this exception, here's how you do it:

    Code:
    try
    {
    //Insert Code that throws the exception here
    }
    catch(ArrayIndexOutOfBoundsException exceptionName)
    {
    //Insert Code to execute only when the problem occurs
    }

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

Featured