Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Practical examples of Try/Catch/Finally
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00836428
Message ID:
00836793
Vues:
20
>I'll usually do that when I >don't< want to display an error to the user. The fact that an error occurred isn't something they should/need to know about. Here's some code from a webpage I've got (the "catch" isn't completely empty, but it easily could have been if I moved some code around):
>
>
>// Grab the querystring "cat" and use it to retrieve
>// the category list.
>// In case of an exception, catch any funky variables
>// and just reset the Category key to 0
>try
>{
>	iCategory = int.Parse(Request.QueryString["cat"]);
>}
>	catch (System.Exception ex)
>{
>	iCategory = 0;
>}
>
>
>The idea behind the code is that if the "cat" querystring variable was changed to something that int.Parse() would choke on (eg. throw an exception), then I reset to a default value. For example, if someone was curious about what would happen if they modified the URL from something like http://www.mywebsite.com/MyPage.ASPX?cat=1 to http://www.mywebsite.com/MyPage.ASPX?cat=ABC, my code won't display any nasty error messages.

This is a great example of where to you Try/Catch. Thank you very much.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform