Solved

how can i get back to tryp block after an exception was thrown?

Posted on 2009-05-06
10
247 Views
Last Modified: 2013-12-17
hello experts,

i loop trought a DateTime[],
while looping i get current element,
i need the precedent elemnet too!
the prob is that for the first element there's no precendent.

i dont want to test if it's first to avoid the IndexOutOfException to raise,

my question:
what can i do, if there's a possibility, in order to continue the process after the Exception was
throws, is there a way to get back  to the try block with incrementing, the index, here 's my i variable?

i add the code,

all infos are welcome.
thanks in advance.
private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

        {

            Drain _tempDrain = null;

            ArrayList _allDrains = new ArrayList();

            int index = 0;
 

            try

            {

                for (int i = 1; i < aDrainDateList.Length; i++ )

                {

                    DateTime current_drain = aDrainDateList[index];

                    DateTime  last_drain = aDrainDateList[index-1];

                    _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                    _allDrains.Add(_tempDrain);

                }

            }catch(IndexOutOfRangeException)

            {

                

            }
 

            return _allDrains;

        }

Open in new window

0
Comment
Question by:toshi_
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 8

Assisted Solution

by:OBonio
OBonio earned 100 total points
ID: 24313692
I would aim not to throw the exception!  Checking whether you are on the first element or not is far better than letting an exception be thrown!

That said, try this :



private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

        {

            Drain _tempDrain = null;

            ArrayList _allDrains = new ArrayList();

            int index = 0;

 

            for (int i = 1; i < aDrainDateList.Length; i++ )

            {

                DateTime current_drain = aDrainDateList[index];

                DateTime  last_drain;

                try

                {

                  last_drain = = aDrainDateList[index-1]; 

                } 

                catch(IndexOutOfRangeException)

                {

                  last_drain = null;

                }
 

                if(last_drain!=null)

                {

                  _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                  _allDrains.Add(_tempDrain);

                }

            }

 

            return _allDrains;

        }

Open in new window

0
 
LVL 16

Assisted Solution

by:CuteBug
CuteBug earned 100 total points
ID: 24313702
Place the try catch block within your for loop and in the catch section you can add continue;
private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

        {

            Drain _tempDrain = null;

            ArrayList _allDrains = new ArrayList();

            int index = 0;

 

            for (int i = 1; i < aDrainDateList.Length; i++ )

            {

                  try

                  {

                    DateTime current_drain = aDrainDateList[index];

                    DateTime  last_drain = aDrainDateList[index-1];

                    _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                    _allDrains.Add(_tempDrain);

                  }

                  catch(IndexOutOfRangeException){

                    continue;

                  }                

            }

 

            return _allDrains;

        }

Open in new window

0
 
LVL 9

Assisted Solution

by:tillgeffken
tillgeffken earned 100 total points
ID: 24313713
From my point of view its bad practice to aim for exceptions, that's why they're called exceptions. What's the purpose of this? Your Drain class requires a last_drain so why not skip it alltogther?

private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

        {

            Drain _tempDrain = null;

            ArrayList _allDrains = new ArrayList();

            int index = 1;

 

            try

            {

                for (int i = 2; i < aDrainDateList.Length; i++ )

                {

                    DateTime current_drain = aDrainDateList[index];

                    DateTime  last_drain = aDrainDateList[index-1];

                    _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                    _allDrains.Add(_tempDrain);

                }

            }catch(IndexOutOfRangeException)

            {

                

            }

 

            return _allDrains;

        }

Open in new window

0
 
LVL 16

Expert Comment

by:CuteBug
ID: 24313722
Obonio's has provided a better solution.
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24313763
I don't see the point in even processing index 0 when you already know that it can only fail.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:CuteBug
ID: 24313764
Another method is to loop backward. This will solve it altogether.

After the loop ends, just reverse the resultant array.
private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

        {

            Drain _tempDrain = null;

            ArrayList _allDrains = new ArrayList();

            int index = 0;

 

            try

            {

                for (int i = aDrainDateList.Length - 1; i > 0; i-- )

                {

                    DateTime current_drain = aDrainDateList[index];

                    DateTime  last_drain = aDrainDateList[index-1];

                    _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                    _allDrains.Add(_tempDrain);

                }

            }catch(IndexOutOfRangeException)

            {

                

            }

 

            // Reverse the order

            Array.Reverse(_allDrains);
 

            return _allDrains;

        }

Open in new window

0
 
LVL 4

Accepted Solution

by:
cauos earned 200 total points
ID: 24313813
yes Obonio's has provided a better solution.but the error occured in the index not index - 1 (he started the for loop from 1 )
 for (int i = 1; i < aDrainDateList.Length; i++ )

            {

                DateTime current_drain;

                DateTime  last_drain =aDrainDateList[index-1];                

                try

                {

                   current_drain = aDrainDateList[index];

                } 

                catch(IndexOutOfRangeException)

                {

                 current_drain= null;

                }

 

                if(current_drain!=null)

                {

                  _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                  _allDrains.Add(_tempDrain);

                }

            }

Open in new window

0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24313847
I still think there is no problem in the first place. You're creating one by trying to parse data of which you know that it doesn't exist. Or am i missing something?
0
 
LVL 4

Expert Comment

by:cauos
ID: 24313937
you right tillgeffken, there is no logic by doing this if he want to return the array list before end the method he can add the finally statement and return the array list.
private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

        {

            Drain _tempDrain = null;

            ArrayList _allDrains = new ArrayList();

            int index = 1;

 

            try

            {

                for (int i = 2; i < aDrainDateList.Length; i++ )

                {

                    DateTime current_drain = aDrainDateList[index];

                    DateTime  last_drain = aDrainDateList[index-1];

                    _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

                    _allDrains.Add(_tempDrain);

                }

            }catch(IndexOutOfRangeException)

            {

                

            }

        finally

         {

            return _allDrains;

         }

        }

Open in new window

0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24314014
Actually i left the Exception in there because i was too lazy to delete it. The following should be just fine (at least you won't get an IndexOutOfRangeException)

private ArrayList GetDrainsObj(DateTime[] aDrainDateList)

{

 Drain _tempDrain = null;

 ArrayList _allDrains = new ArrayList();

 int index = 1;
 

 for (int i = 2; i < aDrainDateList.Length; i++ )

 {

  DateTime current_drain = aDrainDateList[index];

  DateTime  last_drain = aDrainDateList[index-1];

  _tempDrain = new Drain(id_, Ecolog_.Id_int, current_drain, last_drain, DateTime.Now);

  _allDrains.Add(_tempDrain);

 }

 return _allDrains;

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel import error " External table is not in expected format" 12 48
Resolve Dependency Issues 4 41
C# HTTP GET method sample code 3 39
Connection String 16 42
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now