Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Variable scope and try and catch
Message
From
17/09/2003 11:37:18
 
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00829818
Message ID:
00829930
Views:
22
This message has been marked as the solution to the initial question of the thread.
The problem is with scope - you cannot declare variable in TRY and see it in CATCH. This same apply also for VB.

The second problem is with initialization of the variable - you cannot initialize variable in TRY and rely in CATCH that it is initialized, because initialization can also fail. You must initialize it before TRY. This does not apply for VB, because VB internally initializes variables for you.

The same applies for FINALLY. Here is C# workaround:
SqlDataReader dr = null;  //INITIALIZATION

try
{
  ...
  dr = sqlcmd.ExecuteReader();
  ...
}
catch InvalidOperationException ex  //Sample exception handling
{
  ...
}
finally
{
  if(dr != null)
    dr.Close();
}

Plamen Ivanov
MCSD .NET Early Achiever and MCAD .NET Charter Member (VB .NET/SQL Server 2000)
MCSD (VB 6.0/SQL Server 2000)

VB (.NET) - what other language do you need in the whole Universe?...

Previous
Next
Reply
Map
View

Click here to load this message in the networking platform