473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Resume Exception After Handling Exception in C#

Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
.....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Thanks,
Geoffrey
Jan 14 '08 #1
9 2179
But I don't think there is a way in C# to support this pattern.
Correct; there is no resume
I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.
Are you so sure? Have you times it both ways? Since the base-libraries
use mainly bounds-checking, I'm guessing that it isn't as slow as you
think. Of course, exceptions aren't as slow as people think, either...
Of course, the cheapest option is to check your length ahead of time
(outside of any loop).

Marc
Jan 14 '08 #2
To add what Marc says, I don't know that not using an array over a
List<intis going to be faster here either. What you should be doing is
pre-allocating what you think you will use (or over, if you have to
approximate) and you should get pretty even performance.

Of course, the only way to test this is to try.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"beginner" <zy*******@gmail.comwrote in message
news:d3**********************************@j78g2000 hsd.googlegroups.com...
Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Thanks,
Geoffrey

Jan 14 '08 #3
beginner <zy*******@gmail.comwrote:

<snip>
I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.
Not only do I doubt that the performance is likely to be significantly
improved (and there's a big difference between "once in a million" and
the "once in a hundred" you originally state) - there's a massive cost
in readability.

Using exceptions like this is a really, really bad idea. From a
maintenance point of view, it is absolutely horrible. This isn't the
way to get fast code. You should profile the code and attack very
specific bottlenecks, rather than bending your code out of shape like
this without solid performance figures behind you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 14 '08 #4
Hi,

That is the kind fo programming that should not appear in a performance
critical app. It create uncertainly
It's MUCH better if you use a List<Twith enough space to accomodate the
number of items you are expecting.

You will get the same functionality than by using the array and you avoid
creating an exception.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"beginner" <zy*******@gmail.comwrote in message
news:d3**********************************@j78g2000 hsd.googlegroups.com...
Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Thanks,
Geoffrey

Jan 14 '08 #5
On Jan 14, 12:37*pm, Marc Gravell <marc.grav...@gmail.comwrote:
But I don't think there is a way in C# to support this pattern.

Correct; there is no resume
I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Are you so sure? Have you times it both ways? Since the base-libraries
use mainly bounds-checking, I'm guessing that it isn't as slow as you
think. Of course, exceptions aren't as slow as people think, either...
Of course, the cheapest option is to check your length ahead of time
(outside of any loop).

Marc
Unfortunately checking the Length beforehand is impossible as I only
know the ballpark of the number of records I have to process and there
could be big outliers.

You are probably right. In this case, I might be able to banking on
the smartness of the optimizer. If I check the bounds explicitly in
the code, the JIT might not generate code that implicitly does so and
throws exceptions.

But this is still a useful pattern in dealing with things like
numerical computations, which is another situation I am trying to use
this pattern. It is cheaper to handle rare bad situations when it
actually happens. Without the 'resume' mechanism, I will have to redo
the whole function when some exception happens. With the resume, I can
redo at much finer granuality.
Jan 14 '08 #6
I agree with the other responders. At first blush, you seem to be using
exceptions for handling business logic. That's not what they are for. The
idea is to AVOID having exceptions in the first place.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"beginner" wrote:
Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
.....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Thanks,
Geoffrey
Jan 14 '08 #7
Hi,

I see a lot of people dislike this concept, but perhaps I have something
that may help if you want to go down the track of using arrays etc.
You could take out the count2 stuff if it was only one item added per
execution of the loop.

int currentmax=100;
int count=0;
int count2=0; // used to keep a track of the actual number of items in the
array.
int[] l = new int[currentmax];
while (...)
{
if (count2 + 'num of items added each time' currentmax) {
currentmax += 100;
// recreate array with new currentmax size.
}

l[count++]=f(...);
count2++;

l[count*2+1]=f(...);
count2++;
}

- Lucas.

"beginner" <zy*******@gmail.comwrote in message
news:d3**********************************@j78g2000 hsd.googlegroups.com...
Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Thanks,
Geoffrey
Jan 15 '08 #8

"beginner" <zy*******@gmail.comwrote in message
news:d3**********************************@j78g2000 hsd.googlegroups.com...
Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.
Trouble is, the OutOfRange exception isn't thrown using hardware support
(access violation exception using vectored handlers) where you could
actually see a performance gain. Instead, the compiler has to insert the
exact check you are trying to avoid. So check it yourself, then the
optimizer should remove the redundant checks and all the exception baggage.

It would be a different story if using VirtualAlloc to set up your buffer,
so that out of range accesses hit a guard region and cause a page fault.
But in .NET you'd just be accessing the next object on the gc heap, not
faulting.
>
Thanks,
Geoffrey

Jan 18 '08 #9

"Peter Bromberg [C# MVP]" <pb*******@yahoo.NoSpamMaam.comwrote in message
news:41**********************************@microsof t.com...
>I agree with the other responders. At first blush, you seem to be using
exceptions for handling business logic. That's not what they are for. The
idea is to AVOID having exceptions in the first place.
The idea is valid, only doesn't work with .NET arrays. Of course it's an
implementation detail that should be well encapsulated from the user.

Exceptions are designed for implementing the unusual case without loading
down the fast path.

For example, the stack (for parameter passing) uses exactly the technique
the OP is discussing.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"beginner" wrote:
>Hello,

We are writing a performance critical application. As a result, we use
Array instead of List or any other more sophisticated container types
to store temporary data. However, the array type is of fixed length,
and therefore there is a chance for the index to be out of range. I
expect this to happen once every a hundred times of execution and when
this happens, I'd like to create a bigger array. To prevent the bounds
from being checked every time, I am thinking of the following code (in
sketch):

int [] l= new int[100]; .//temp variable
.....
try
{

int count=0;
while(...)
{
...
l[count++]=f(...); //
....
l[count*2+1]=f(...); //multiple places that put stuff into the
array
}

}
catch(OutOfRange e)
{
//create a bigger array
l2=new int[count*2+100];
//copy partial results from old array to new array
...
//replace old array
l=l2
resume execution right after the exception was thrown
}

But I don't think there is a way in C# to support this pattern.

I think this is a really useful pattern, as it is way more efficient
to deal with such conditions that happen once in a million times.

Thanks,
Geoffrey

Jan 18 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Boogie El Aceitoso | last post by:
Hi, Is it possible to 'fix'in the catch block whatever caused an exception and return to the offending statement in the try block, and try it again? After raising an exception, I'd like to try...
5
by: Rob R. Ainscough | last post by:
Coming from VB6 to VB.NET, it appears if I opt to use the Try Catch approach I don't have any way to RESUME from within my catch statement? What I often do is resolve the problem in my catch...
2
by: juky | last post by:
Hi all, I have a loop in the thread checking for a particular service status, whenever the status changes to "stopped" a RaiseEvent is generated by thread and another function runs. At the same...
7
by: Michael | last post by:
Hi Everyone, I'm using VS 2005 now and would like to know if .Net has a replacement for the Resume Next clause. I would like to do something like the following: On Error Resume Next value =...
15
by: Neo | last post by:
Hello All, Although, I have read all the advantages of using Try Catch Block instead of "On error goto", I am still confused what is alternative for classic "Resume" statement. "Resume" was one...
7
by: fniles | last post by:
In VB 6.0 in the error trapping, we can do "resume next" to continue on the next code. How can we do that in .NET with "Try", "Catch","End Try" ? Thanks
7
by: Rob R. Ainscough | last post by:
In VB6 I can use Resume Next to execute the line of coding following the line that cause an exception. There doesn't appear to be anything similiar when using Try...Catch -- so how can one resume...
8
by: Adil Akram | last post by:
There are situations where you want to retry/rerun the same code again (if user wants) for unlimited times for a particular exception thrown. For example in situations like there's no CD in the...
11
by: fniles | last post by:
In VB 6 I can do the following: Sub MySub on error goto Err1 : --all my codes are here : --say this is where the error occurs : --this is where resume next will bring me after Err1 exit sub...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.