private void Test() { IPEndPoint ipEP; Socket udpSocket; try { // some code 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 { //I need some code here to test if the socket is created/initialized if (udpSocket !=null) udpSocket.Close(); } }>Del and Stephane,
>Socket udpSocket = new Socket(ipEP.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp)
>
>and if I initialize the Socket before the try block I am SOL if the initilazation of the Socket throws an error.>>>int myInt; >>>try >>>{ >>> myInt = 1; >>>} >>>finally >>>{ >>> if (myInt != 1) >>> { >>> myInt = -4; >>> } >>>} >>>Console.WriteLine(myInt.ToString()); >>>>>>
>>>>Object myObject; >>>>try >>>>{ >>>> myObject = new SomeThing(); >>>>... >>>>} >>>>finally >>>>{ >>>> if (myObject !=null) >>>> {...} >>>>} >>>>>>>>