Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations danielledunham on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Catch Block Handling

Status
Not open for further replies.

bam720

Technical User
Joined
Sep 29, 2005
Messages
289
Location
US
I've never really used the catch block before and am having some trouble with it. When I run my program and it crashes here I see the message box, click OK, and then another box is spawned, and another, and another ... etc until it finally crashes. Am I missing something, I just want it to return to where it was as if the user hasn't done anything. The function it is in, just tries to read a file and populate a table, so there is no "state" information or anything.

Code:
catch(Exception e) {
	MessageBox.Show("Error: ShowChecks");				return;
}
 
I do not use C#, so i need some reference first to answer you.
I think that the return statement does it. Try removing it.
 
I tired removing the return line and the same thing happened.
 
Code:
private void ShowChecks(){
try {
	int i;
	string[] Items;
	Regex Delim = new Regex(AspexType.Delim.ToString());
	for(i = 0;i < OpenChecks.Count;i++) {
		string OC = Convert.ToString(OpenChecks[i]);
		Items = Delim.Split(OC);
		GridChecks.SetData(i, 1, DelphiDoubleToString(Convert.ToDouble(Items[0])));  //Check ID
		GridChecks.SetData(i, 0, Items[1]);  //Table #
		GridChecks.SetData(i, 2, Items[2]);  //Subtotal
		GridChecks.SetData(i, 3, Items[3]);  //AccountID
		if(OpenChecks.Count == GridChecks.Rows.Count) {
			GridChecks.Rows.Add();
			}
		}    			
	}
catch(Exception e) {
	MessageBox.Show("Error: ShowChecks");
	//return;
        }
}
 
What is the exception you are getting?

And how is this method called?
 
This function is called when I press a button on another page. Right Now it's Crashing "AruguementOutOFRangeException" then the program will die becuase of Stack Overflow
 
That AruguementOutOFRangeException Exception tells you that pobably an array index is greater than or equal to the array's items (count). In other words, are you sure that Items[3] exists?
 
TipGiver, I understand what the error means, and what is causing it. I'm incrementing OpenChecks.Count When I click something else and then there are no records at that point. I was just curious to see if it was possible for the catch block to "exit gracefully
 
its probably not good practice, but I've used this before, I call it the "Catch and Release" (I always think of fishing when I do it)

Code:
try
{
   blah.blah(blahBlah);
}
catch
{
   // and release...
}

But since you are expecting a specific error, it would probably be a little better to catch and release the exact error you want, then handle all others differently, i.e. :

Code:
try
{
   blah.blah(blahBlah);
}
catch(ArgumentOutRangeException e)
{
   // and release...
}
catch(Exception e2)
{
   MessageBox.Show(e2.Message);
}
 
NeilTrain,
Thanks for the help, and I see what you mean. Currently I am in debugging and working out how to fix the error, so I want it to be generic still. I'm just having trouble with the release mechanism. The Error is being thrown about 20x. Shouldn't the exception handler catch the first ArgOutOfRange and then stop the function from executing any further? Again I appreciate the help.
 
Shouldn't the exception handler catch the first ArgOutOfRange and then stop the function from executing any further?
uhh yes I'm pretty sure thats how it should work. Are you sure its not refiring the same method? I doubt its trying to continue to the next line after each exception.

Are you using visual studio? Can you step through your code and see if its actually exiting the try/catch block after each exception?
 
The StackOverflowException is usually because of uncontrolled recursion. You might want to examine your button's event handler and make sure there's not a cycle in there.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top