Wrox Programmer Forums
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 29th, 2005, 02:30 AM
Authorized User
Join Date: Jan 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to JelfMaria
Default Innerexception

Can anyone explain with sample the use of innerexception property in Exception handling.I cant understand its real usage.

Old June 29th, 2005, 07:46 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

Let's say we have a class called "WidgetBuilder". We have a method on it to make a Widget: WidgetBuilder.MakeOne(). If something goes wrong inside this method, I throw a custom exception: WidgetException. Now let's say that part of making a Widget is to call the database. If something goes wrong, the methods that I'm calling will throw exceptions specific to their operations (such as a SqlException). Now, part of my code in WidgetBuilder.MakeOne() is a try...catch block. When calls to the database operations fail and throw SqlExceptions, I'll catch them and throw my own WidgetException. Wouldn't it be handy to be able to include the exception that originally happened inside my WidgetException so that I can look thru the exception stack and see where the problem originally occurred. Hence the reason for Exception.InnerException.

  SqlClient.SqlConnection objConn = new SqlClient.SqlConnection();
  //this will definately break without a connection string
catch (SqlException e){
  throw new WidgetException("Sorry, couldn't build the widget.", e);


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.