EDN Admin
Well-known member
Hello,
Ive recently had an issue with Visual Studio 2010 where when launching a project from within the IDE, instead of breaking on an exception and alerting me to the code that threw the exception, the application will simply end. I did some research by googling
and found that on Windows 7 x64 this has to do with a change made that prevents the exception from making it back through to the attached debugger (in this case, the visual studio debugger). I also read that I can go to "Debug -> Exceptions" and
change some settings to re-enable this function. However, upon doing so it breaks on
every exception, even if it occurs in a Try/Catch where the specific exception thrown is handled. This makes testing Try/Catch very, very annoying as I have to run the application outside the IDE and if it doesnt work, have to guess as to
the problem.
An example...
<pre lang="x-c# //* get our server
cServer selected = this.servers[this.cbServers.SelectedIndex];
//* create a new csocket object
cSocket connection = new cSocket(selected.IP, selected.Port);
//* it should resolve the IP on its own in (cSocket.GetIPAddress(string ip))
//* lets connect
Retry:
try
{
//* we use a try/catch because we expect certain socketexceptions to be thrown (ip/port stored in the cSocket class)
connection.Connect();
}
catch (SocketException error)
{
MessageBox.Show(error.SocketErrorCode.ToString());
//* what if the computer they try to connect to isnt accepting?
if (error.SocketErrorCode == SocketError.ConnectionRefused)
{
//* alert the user
DialogResult answer = MessageBox.Show("The server actively refused the connection attempt.nnMake sure the IP Address or Hostname and port are correct.nn" + error.Message, "Error connecting...", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
if (answer == System.Windows.Forms.DialogResult.Retry)
{
//* they want to try the connection again
goto Retry;
}
//* kill the socket
connection.Socket.Close(0);
connection.Socket.Dispose();
connection = null;
}
}[/code]
The above code (with the changes suggested on websites) will not pop the Catch part of the code. If ran outside the IDE it will. With no changes applied, it sometimes will pop the Catch code.
The other problem Im having is actually part of the same above code. The "connection.Connect()" calls Socket.BeginConnect();. This, from my understand, starts a new thread in which the connection attempt is made. The code in the cSocket class
is as follows:
<pre lang="x-c# //* this Connect() assumes that the ip and port returned from this.IP / this.IPString and this.Port will be used
public void Connect()
{
//* if were using the string, try to get the IP again
if (this.UseString)
this.IP = this.GetIPAddress(this.IPString);
//* if no ip was returned, throw an exception
if (this.IP == null)
{
//* return but alert the user
MessageBox.Show("An invalid IP address was supplied. Connection attempt aborted.", "Error connecting...", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//* all connections are done asyncronously
this.Socket.BeginConnect(new IPEndPoint(this.IP, this.Port), this.EndConnect, this);
}
//* finishes an asyncronous connection attempt
private void EndConnect(IAsyncResult result)
{
this.Socket.EndConnect(result);
//* now begin listening for incoming data
this.Receive();
}[/code]
Now, if the remote computer refuses the connection, no exception is caught. However, if I put the Try/Catch in the class code, it will catch the exception. Can exceptions from the Async threads only be caught on the thread they occurred? I would assume if
I want to respond on the main thread, I would have to use delegates to do so (hence want to catch it on the main thread).
Edit: Realizing I didnt really ask the questions I wanted: Is there any way to get the old breaking on exceptions back and is there any way to catch the exception from the origin thread instead of the thread the exception occurs?
View the full article
Ive recently had an issue with Visual Studio 2010 where when launching a project from within the IDE, instead of breaking on an exception and alerting me to the code that threw the exception, the application will simply end. I did some research by googling
and found that on Windows 7 x64 this has to do with a change made that prevents the exception from making it back through to the attached debugger (in this case, the visual studio debugger). I also read that I can go to "Debug -> Exceptions" and
change some settings to re-enable this function. However, upon doing so it breaks on
every exception, even if it occurs in a Try/Catch where the specific exception thrown is handled. This makes testing Try/Catch very, very annoying as I have to run the application outside the IDE and if it doesnt work, have to guess as to
the problem.
An example...
<pre lang="x-c# //* get our server
cServer selected = this.servers[this.cbServers.SelectedIndex];
//* create a new csocket object
cSocket connection = new cSocket(selected.IP, selected.Port);
//* it should resolve the IP on its own in (cSocket.GetIPAddress(string ip))
//* lets connect
Retry:
try
{
//* we use a try/catch because we expect certain socketexceptions to be thrown (ip/port stored in the cSocket class)
connection.Connect();
}
catch (SocketException error)
{
MessageBox.Show(error.SocketErrorCode.ToString());
//* what if the computer they try to connect to isnt accepting?
if (error.SocketErrorCode == SocketError.ConnectionRefused)
{
//* alert the user
DialogResult answer = MessageBox.Show("The server actively refused the connection attempt.nnMake sure the IP Address or Hostname and port are correct.nn" + error.Message, "Error connecting...", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);
if (answer == System.Windows.Forms.DialogResult.Retry)
{
//* they want to try the connection again
goto Retry;
}
//* kill the socket
connection.Socket.Close(0);
connection.Socket.Dispose();
connection = null;
}
}[/code]
The above code (with the changes suggested on websites) will not pop the Catch part of the code. If ran outside the IDE it will. With no changes applied, it sometimes will pop the Catch code.
The other problem Im having is actually part of the same above code. The "connection.Connect()" calls Socket.BeginConnect();. This, from my understand, starts a new thread in which the connection attempt is made. The code in the cSocket class
is as follows:
<pre lang="x-c# //* this Connect() assumes that the ip and port returned from this.IP / this.IPString and this.Port will be used
public void Connect()
{
//* if were using the string, try to get the IP again
if (this.UseString)
this.IP = this.GetIPAddress(this.IPString);
//* if no ip was returned, throw an exception
if (this.IP == null)
{
//* return but alert the user
MessageBox.Show("An invalid IP address was supplied. Connection attempt aborted.", "Error connecting...", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//* all connections are done asyncronously
this.Socket.BeginConnect(new IPEndPoint(this.IP, this.Port), this.EndConnect, this);
}
//* finishes an asyncronous connection attempt
private void EndConnect(IAsyncResult result)
{
this.Socket.EndConnect(result);
//* now begin listening for incoming data
this.Receive();
}[/code]
Now, if the remote computer refuses the connection, no exception is caught. However, if I put the Try/Catch in the class code, it will catch the exception. Can exceptions from the Async threads only be caught on the thread they occurred? I would assume if
I want to respond on the main thread, I would have to use delegates to do so (hence want to catch it on the main thread).
Edit: Realizing I didnt really ask the questions I wanted: Is there any way to get the old breaking on exceptions back and is there any way to catch the exception from the origin thread instead of the thread the exception occurs?
View the full article