Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Finally code
Message
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
00970841
Message ID:
00970971
Views:
20
I think I found the syntax for the finally block, but it will not compile
try
{
  // some code
  IPEndPoint ipEP = new IPEndPoint(paraIPAddress, iPort);
  Socket udpSocket = new Socket(ipEP.Address.AddressFamily,SocketType.Dgram,ProtocolType.Udp);
  // some more code
}
catch(Exception ex)
{
  //Do something if exception is thrown
}
finally
{
  if (udpSocket != null)
  {
    udpSocket.Close();
  }
}
This code gives me the error that udpSocket does not exist in the namespace, so I moved the declaration statement outside the try..catch..finally block.
Socket udpSocket;
try
{
  // some code
  IPEndPoint ipEP = new IPEndPoint(paraIPAddress, iPort);
  udpSocket = new Socket(ipEP.Address.AddressFamily,SocketType.Dgram,ProtocolType.Udp);
  // some more code
}
catch(Exception ex)
{
  //Do something if exception is thrown
}
finally
{
  if (udpSocket != null)
  {
    udpSocket.Close();
  }
}
Now this code gives me the compile error use of unassigned local variable udpSocket

Any hints or comments welcome.

Thanks,
Einar
Semper ubi sub ubi.
Previous
Reply
Map
View

Click here to load this message in the networking platform