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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

try catch within a catch 2

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
What happens when you put a try catch within another catch? I need to do this because on catching the an error, I need to write data to a table, which will also require a try catch to catch a db error.

Any help would be really appreciated.


Cheers,

lfc77
 
lfc77,
I'm not sure if I've seen nested try/catch blocks, but you can use the following suggestion. Instead of writing the data from within the method where the main try/catch block is, create a function that writes data to the database and returns true or false depending on whether the write was successful or not. Then, in the main try/catch block, call such function and take action depending on its return value. Something like this:
Code:
[green]// Main try/catch block[/green]
[blue]try[/blue]
{
  [i]statement1;
  statement2;
     .
     .
     .
  statnementn;[/i]
}
[blue]catch[/blue] ([i]ExpectedException[/i] e)
{
  [green]// Call function to write to db[/green]
  [blue]bool[/blue] writeOK = WriteToDB(...);
  [blue]if[/blue] (writeOK)
    [green]// Everything went OK[/green]
  [blue]else[/blue]
    [green]// Writing to the database failed[/green]
}
[blue]finally[/blue]
{...}


[green][b]// WriteToDB function[/b][/green]
[blue]private bool[/blue] WriteToDB(...)
{
  [blue]try[/blue]
  {
    [i]statement1;
    statement2;
       .
       .
       .
    statementn;[/i]
    [blue]return true[/blue];
  }
  [blue]catch[/blue] ([i]SomeDataBaseException[/i] ex)
  {
    [green]...[/green]
    [blue]return false[/blue];
  }
}
Hope this helps!

JC

_________________________________________________
To get the best response to a question, read faq222-2244!
 
As far as I can tell, there is nothing wrong with nesting 2 trys....I added a second try/catch inside of my catch, after you asked, and I received no errors...though, you may want to consider writing your errors to a file, just in case your database connection stuff bombs. Especially if you connect over a network. For example, if I try to connect to a table on my oracle database and, for some reason, the network goes down, I would receive an oracle error and when my catch tried to connect over the network to write the errors to a table, it would catch another failure to connect to the database.

Just an idea,

Good luck,

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts
 
Yes, you can do that like you do for if then else endif.
Here is an example.
System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection();
string sErr = "";
string s1 = null;
try
{
int i1 = s1.Length;
}
catch (Exception e1)
{
sErr+=e1.GetType() + e1.Message;
// process here the null reference thrown be calling Length on s1
try
{
sqlConnection1.Open();
}
catch (Exception e2)
{
sErr+= e2.GetType() + e2.Message;
// process here the exception thrown by calling Open on a not enough initialized object

}

}
finally
{
//Log(sErr);

}

-obislavu-
 
Kevin,
Thanks for the suggestion. Having nested try/catch blocks doesn't sound like the cleanest code, but the idea of writing the database to a file is a good one. In fact, what lfc77 should do is write the error to a file when the the WriteToDB function returns false. Something like this:
Code:
[green]// In the main try/catch block...[/green]
[blue]catch[/blue] ([i]ExpectedException[/i] e)
{
  [green]// Call function to write to db[/green]
  [blue]bool[/blue] writeOK = WriteToDB(...);
  [blue]if[/blue] (writeOK)
  {
    // Everything went OK
  }
  [blue]else[/blue]
  {
    [green][b]// Writing to the database failed
    // Write to a file instead[/b][/green]
  }
}
[green]...[/green]
Have a star Kevin!

JC

_________________________________________________
To get the best response to a question, read faq222-2244!
 
Good looking out JC, that was definitely a better way of handling it than nesting the try/catch blocks. I would say that you probably deserve the star more than I.

Good luck ifc77, you could do far worse than taking JC's advice, he's helped me through several spots.

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top