There are situations where you really cannot escape Try/Catch,
Consider a situation where you want the user to enter some data to enter into a database table. User input also includes a primary key. Now, if any user enters a duplicate primary key, you will get an exception. What would you like to do now? let the system fail or handle the exception and show the user a message convenient for the user to enter something else / unique.
. , , - URL, - : ( )
private bool IfURLExists(string url)
{
try
{
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
return false;
}
}
Try/Catch.
try/finally using, using , IDisposable try/finally, , , .
, , - ,