For Programmers: Free Programming Magazines  


Home > Archive > Smartphone Developer Forum > February 2005 > IrDA Socket exception









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author IrDA Socket exception
ukpauls

2005-02-09, 4:00 pm

The following method dies at line: (client = new IrDAClient(serviceName);)
when there are no devices found and crashes the program, why does the catch
not work????????





private void sendIRData(byte[] sendingBuffer, int bufferLength)
{
IrDAClient client = null;
int currentTries = 0;

do
{
try
{
client = new IrDAClient(serviceName);
}
catch(SocketException e)
{
if (currentTries >= 5)
{
throw e;
}
}
currentTries = currentTries + 1;
}
while ((client == null) && (currentTries < 5));

if (client == null)
{
throw new SocketException();
}
System.IO.Stream stream = null;

try
{
stream = client.GetStream();
stream.Write(sendingBuffer,0,bufferLength);
}
finally
{
if (stream != null)
{
stream.Close();
}
client.Close();
}
}
Brian Cross [MSFT]

2005-02-10, 9:00 am

looks like your code tries the line you mention: (client - new
IrDAClient(serviceName);) until currentTries is >= 5.
Then you check to see if client == null (which it will since IrDAClient
failed each time) and you throw a new SocketException. if you don't have a
try block around sendIRData, then this SocketException will be unhandled and
crash your program.

--
Brian Cross
Developer Experience, Windows Mobile

This posting is provided AS IS with no warranties, and confers no rights.


"ukpauls" <ukpauls@discussions.microsoft.com> wrote in message
news:B4BA360D-94DB-47F4-A5A1-A6213D1B58CE@microsoft.com...
> The following method dies at line: (client = new IrDAClient(serviceName);)
> when there are no devices found and crashes the program, why does the
> catch
> not work????????
>
>
>
>
>
> private void sendIRData(byte[] sendingBuffer, int bufferLength)
> {
> IrDAClient client = null;
> int currentTries = 0;
>
> do
> {
> try
> {
> client = new IrDAClient(serviceName);
> }
> catch(SocketException e)
> {
> if (currentTries >= 5)
> {
> throw e;
> }
> }
> currentTries = currentTries + 1;
> }
> while ((client == null) && (currentTries < 5));
>
> if (client == null)
> {
> throw new SocketException();
> }
> System.IO.Stream stream = null;
>
> try
> {
> stream = client.GetStream();
> stream.Write(sendingBuffer,0,bufferLength);
> }
> finally
> {
> if (stream != null)
> {
> stream.Close();
> }
> client.Close();
> }
> }



ukpauls

2005-02-10, 9:00 am

Do you man in the method that calls sendIRdata in need a try block??? or you
just saying it should already work? the way it is?

"Brian Cross [MSFT]" wrote:

> looks like your code tries the line you mention: (client - new
> IrDAClient(serviceName);) until currentTries is >= 5.
> Then you check to see if client == null (which it will since IrDAClient
> failed each time) and you throw a new SocketException. if you don't have a
> try block around sendIRData, then this SocketException will be unhandled and
> crash your program.
>
> --
> Brian Cross
> Developer Experience, Windows Mobile
>
> This posting is provided AS IS with no warranties, and confers no rights.
>
>
> "ukpauls" <ukpauls@discussions.microsoft.com> wrote in message
> news:B4BA360D-94DB-47F4-A5A1-A6213D1B58CE@microsoft.com...
>
>
>

Brian Cross [MSFT]

2005-02-10, 4:01 pm

If you want to handle the SocketException that you are throwing, then yes,
you need a try block around the call to sendIRData.

the code you have:
if (client == null)
{
throw new SocketException();
}

does not have a try/catch around it in the snippet that you had in your
post. If client == null at that point, you will throw a new
SocketException. because there is not a try/catch around it, it will go up
the stack to the method that called sendIRData, If there is no try/catch
around the call to sendIRData, the exception will go up the stack to the
method that called that method, etc, etc until either a try/catch that
handles SocketException, or the end of your app.


--
Brian Cross
Developer Experience, Windows Mobile

This posting is provided AS IS with no warranties, and confers no rights.

"ukpauls" <ukpauls@discussions.microsoft.com> wrote in message
news:4D214F28-91BD-4EF2-963D-85A39276E3BF@microsoft.com...[color=darkred]
> Do you man in the method that calls sendIRdata in need a try block??? or
> you
> just saying it should already work? the way it is?
>
> "Brian Cross [MSFT]" wrote:
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com