A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
You can use ErrorCode property and Errors collection to find out more about why exception was given. It's possible you find error code that goes for group of errors that have similar consequences. I suggest you to better go with these error codes and not
with error messages because messages can come from resource files.
Don't forget to mark solution providing post as "Answered".
The main point is that SqlException provides you with error codes and instead of guessing errors this way you can instead focus on whole error groups. Based on this information you say to user on general manner what went wrong.
Anyway you have to log exception details to some safe place because sometimes messages given with exceptions contain sensitive data like usernames or network addresses.
Don't forget to mark solution providing post as "Answered".
grwlgrv
Member
51 Points
74 Posts
Custom Exception
Feb 03, 2013 02:37 AM|LINK
I simply want to catch exception "
A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
"
So that i can promt my own messege.
Cynikal
Member
506 Points
117 Posts
Re: Custom Exception
Feb 03, 2013 02:47 AM|LINK
You can catch the exception by using a
try { //Code to try..if it errors, it will be caught below. } catch (exception ex) { //Catch the exception here... }If you want to post your own message in a label or something, in the catch exception portion, just put label1.text = "Whatever";
If you want to throw a new exception...
Throw new Exception("Your custom exception text here");grwlgrv
Member
51 Points
74 Posts
Re: Custom Exception
Feb 03, 2013 03:07 AM|LINK
i want like:
try
{
}
catch(//transport level error)
{
}
catch(exception ex)
{
}
Could you please suggest me anything like this
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: Custom Exception
Feb 03, 2013 03:29 AM|LINK
You cannot catch exception by message but by type. In your case it is SqlException that is fully documented here.
try { // do something } catch(SqlException ex) { // handle exception }You can use ErrorCode property and Errors collection to find out more about why exception was given. It's possible you find error code that goes for group of errors that have similar consequences. I suggest you to better go with these error codes and not with error messages because messages can come from resource files.
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
sreejukg
All-Star
27607 Points
4122 Posts
Re: Custom Exception
Feb 03, 2013 03:30 AM|LINK
try { } catch(SqlException sqlError) { } catch(Exception ex) { }My Blog
Cynikal
Member
506 Points
117 Posts
Re: Custom Exception
Feb 03, 2013 03:56 AM|LINK
the exception ex one is for a general exception. Mostly everything will be caught through this...
If you know the error message, you could then do an if statement.
if (ex.ToString().Contains("error message")) { //Do something here }DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: Custom Exception
Feb 03, 2013 08:40 AM|LINK
And what if message changes because of driver upload or what if some machine uses different language settings so error messages are translated?
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
Cynikal
Member
506 Points
117 Posts
Re: Custom Exception
Feb 03, 2013 02:06 PM|LINK
Clearly, one would properly test their software to see if the error message arrises.
If its on his server, the error will be the same to everyone.
Setting a exception for a specific error message is never the best sollution, and I would of figure'd you would of known that.
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: Custom Exception
Feb 03, 2013 02:38 PM|LINK
The main point is that SqlException provides you with error codes and instead of guessing errors this way you can instead focus on whole error groups. Based on this information you say to user on general manner what went wrong.
Anyway you have to log exception details to some safe place because sometimes messages given with exceptions contain sensitive data like usernames or network addresses.
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
grwlgrv
Member
51 Points
74 Posts
Re: Custom Exception
Feb 17, 2013 10:36 AM|LINK
Thanks, it works