Theory of catching errors (C#)

Soldato
Joined
16 Nov 2003
Posts
9,682
Location
On the pale blue dot
Hi guys. Here's a bit of pseudo-code:

Code:
try
{
  result = ConnectToSql(query);
}
catch queryException
{
  return queryException;
}
print result; // <- This fails
As the result variable is defined within the catch block, it can't be used outside of it. I could get around this by putting all my code that uses the result data inside of the try block, but I think this isn't very elegant. How would you approach this?
 
Thanks guys, declaring the variable outside of the try block sounds so obvious when you think about it! In general though I suppose you have to be wary of the variable's contents if the call to SQL failed.

Regarding printing the error, this is just me writing bad pseudo-code! In the app itself the call to SQL is critical to the running of the program. So if the call fails and is caught my own logging class writes the failure reason to the console and a log file and then dies, so the last print result call will either not be executed because the call fails or it will have a value.

Thanks!
 
Back
Top Bottom