.NET Tip: Exiting a Try/Catch Block Wisconsin

If you use a return statement within a Try/Catch block, there's a behavior you need to be aware of in your code.

Local Companies

Acumium
608 310 9700 x 522
5133 West Terrace Drive Suite 300
Madison, , WI
Xorbix Technologies Inc.
414-277-5044
759 N. Milwaukee St.
Milwaukee, WI
R.E. Coker and Associates, Inc.
262-723-8104
108 W Court St.
Elkhorn, WI
Visionary Computer Solutions
262-365-9430
PO Box 406
Grafton, WI
R S InfoCon, Inc.
262-898-7456
2320 Renaissance Blvd
Sturtevant, WI
HarrisData
262-784-9099
13555 Bishop's Court, Suite 300
Brookfield, WI
Wireless Direct
1.866.707.8498
BOX 71101
shorewood, WI
IFS
414-577-5191
12000 W. Park Place
Milwaukee, WI
Jelltech Consulting
(262) 373-1350
21180 W Capitol Dr
Pewaukee, WI
Larson Consulting
(920) 346-5876
127 E Main St
Brandon, WI


.NET Tip: Exiting a Try/Catch Block

provided by: 
Originally published at Internet.com


If you use a return statement within a Try/Catch block, there's a behavior you need to be aware of in your code. Consider this block, for example: try { DoSomething(); return; } catch (Exception ex) { // Handle exception here } // code continues here...

Assuming no exception is generated, the return statement will fire normally and the code flow will go back to whatever routine called this block of code. Adding a Finally section to this block of code, however, can lead to confusion. Take this code, for example: try { Console.WriteLine("In try block"); return; } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.WriteLine("In finally block"); }

Even though the return statement normally will send you back to the calling block of code, the finally block always executes. For this snippet, the output will look like this: In try block In finally block

This behavior is by design, but it's just something to remember if you're using finally blocks along with return statements in your exception handling.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Wireless Direct

1.866.707.8498
BOX 71101
shorewood, WI
http://www.prepaidwireless.2ya.com

Regional Articles
- .NET Tip: Exiting a Try/Catch Block Appleton WI
- .NET Tip: Exiting a Try/Catch Block Baraboo WI
- .NET Tip: Exiting a Try/Catch Block Beaver Dam WI
- .NET Tip: Exiting a Try/Catch Block Beloit WI
- .NET Tip: Exiting a Try/Catch Block Brookfield WI
- .NET Tip: Exiting a Try/Catch Block Burlington WI
- .NET Tip: Exiting a Try/Catch Block Cedarburg WI
- .NET Tip: Exiting a Try/Catch Block Chippewa Falls WI
- .NET Tip: Exiting a Try/Catch Block Cudahy WI
- .NET Tip: Exiting a Try/Catch Block De Pere WI
- .NET Tip: Exiting a Try/Catch Block Delavan WI
- .NET Tip: Exiting a Try/Catch Block Eau Claire WI
- .NET Tip: Exiting a Try/Catch Block Elkhorn WI
- .NET Tip: Exiting a Try/Catch Block Fond Du Lac WI
- .NET Tip: Exiting a Try/Catch Block Fort Atkinson WI
- .NET Tip: Exiting a Try/Catch Block Franklin WI
- .NET Tip: Exiting a Try/Catch Block Green Bay WI
- .NET Tip: Exiting a Try/Catch Block Hartland WI
- .NET Tip: Exiting a Try/Catch Block Janesville WI
- .NET Tip: Exiting a Try/Catch Block Kaukauna WI
- .NET Tip: Exiting a Try/Catch Block Kenosha WI
- .NET Tip: Exiting a Try/Catch Block La Crosse WI
- .NET Tip: Exiting a Try/Catch Block Lake Geneva WI
- .NET Tip: Exiting a Try/Catch Block Manitowoc WI
- .NET Tip: Exiting a Try/Catch Block Marinette WI
- .NET Tip: Exiting a Try/Catch Block Marshfield WI
- .NET Tip: Exiting a Try/Catch Block Menasha WI
- .NET Tip: Exiting a Try/Catch Block Menomonee Falls WI
- .NET Tip: Exiting a Try/Catch Block Menomonie WI
- .NET Tip: Exiting a Try/Catch Block Merrill WI
- .NET Tip: Exiting a Try/Catch Block Middleton WI
- .NET Tip: Exiting a Try/Catch Block Milwaukee WI
- .NET Tip: Exiting a Try/Catch Block Mosinee WI
- .NET Tip: Exiting a Try/Catch Block Mukwonago WI
- .NET Tip: Exiting a Try/Catch Block Muskego WI
- .NET Tip: Exiting a Try/Catch Block Neenah WI
- .NET Tip: Exiting a Try/Catch Block New Berlin WI
- .NET Tip: Exiting a Try/Catch Block Oak Creek WI
- .NET Tip: Exiting a Try/Catch Block Oconomowoc WI
- .NET Tip: Exiting a Try/Catch Block Onalaska WI
- .NET Tip: Exiting a Try/Catch Block Oshkosh WI
- .NET Tip: Exiting a Try/Catch Block Pewaukee WI
- .NET Tip: Exiting a Try/Catch Block Racine WI
- .NET Tip: Exiting a Try/Catch Block Rhinelander WI
- .NET Tip: Exiting a Try/Catch Block Rice Lake WI
- .NET Tip: Exiting a Try/Catch Block River Falls WI
- .NET Tip: Exiting a Try/Catch Block Schofield WI
- .NET Tip: Exiting a Try/Catch Block Shawano WI
- .NET Tip: Exiting a Try/Catch Block Sheboygan WI
- .NET Tip: Exiting a Try/Catch Block South Milwaukee WI
- .NET Tip: Exiting a Try/Catch Block Stevens Point WI
- .NET Tip: Exiting a Try/Catch Block Sturgeon Bay WI
- .NET Tip: Exiting a Try/Catch Block Sun Prairie WI
- .NET Tip: Exiting a Try/Catch Block Superior WI
- .NET Tip: Exiting a Try/Catch Block Thiensville WI
- .NET Tip: Exiting a Try/Catch Block Two Rivers WI
- .NET Tip: Exiting a Try/Catch Block Watertown WI
- .NET Tip: Exiting a Try/Catch Block Waukesha WI
- .NET Tip: Exiting a Try/Catch Block Waupaca WI
- .NET Tip: Exiting a Try/Catch Block Wausau WI
- .NET Tip: Exiting a Try/Catch Block West Bend WI
- .NET Tip: Exiting a Try/Catch Block Whitewater WI
- .NET Tip: Exiting a Try/Catch Block Wisconsin Rapids WI
Related Local Events
2008 Early Stage Symposium
Dates: 11/5/2008 - 11/6/2008
Location: Monona Terrace
Madison WI
View Details

Wisconsin Entrepreneurs' Conference
Dates: 6/9/2008 - 6/10/2008
Location: Hyatt Regency Hotel
Milwaukee WI
View Details
Rate Article
     
Articles Insider

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Advertising Engineering Industrial Goods & Services Software
Business Services Family Insurance Technology
Career Financial Services Internet Telecommunications
Cars Food & Beverage Legal Transportation & Logistics
Computer Hardware Health Real Estate Travel
Construction Home Services Retail & Consumer Services Wedding
Education