Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Finally code
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Divers
Thread ID:
00970841
Message ID:
00970971
Vues:
19
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.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform