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 27th, 2005, 01:10 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 Nested Exceptions in c#

i want the output like this in c#:

Outer try running..
Nested try running...
Nested catch caught an error
Nested finally is running...
Outer catch caught an error re-thrown
Outer finally running

the following code i wrote is showing error "type caught/thrown shud be dervied from system.exception".
Can anyone help me with the complete code


try
{
Console.WriteLine ("Outer try running...");
try
{
Console.WriteLine("Nested try running...");
throw "an error";
}
catch(Exception e)
{
Console.WriteLine("Nested catch caught " + e);
throw e + " re-thrown";
}
finally
{
Console.WriteLine("Nested finally is running...");
}
}
catch(Exception e)
{
Console.WriteLine("Outer catch caught " + e);
}
finally
{
    Console.WriteLine("Outer finally running");
}
}

Thanks
Jelfy
__________________
Thanks
Jelfy
 
Old June 27th, 2005, 02:55 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Jelfy,

The problem is caused by this:

throw e + " re-thrown";

You cannot just concatenate text to an Exception object. To fix this, you have a few options:

1. Use they keyword Throw and nothing else. That passes up the original exception and leaves the call stack in tact

2. Create a brand new exception and throw that.

3. Create a brand new exception and wrap the original exception in it. One of the overloaded constructors of an Exception expects another exception.

HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 27th, 2005, 04:55 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

Thanks a lot.
The code is working now.
But how can i throw some string value along with the object e?

like throw e + "Hai"

Thanks
Jelfy
 
Old June 27th, 2005, 05:04 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Like I said, you'll need to create a new exception and, optionally, wrap the original exception in it:

try
{
  // Your code here
}
catch (Exception ex)
{
  throw new Exception(ex.Message + "Something", ex);
}

Instead of throwing a generic Exception, you may want to throw a more specific exception.

The code that catches your new exception can now get the message like this:

  ex.Message //(includes the original message + "Something")

and get at the original exception like this:

  ex.InnerException

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old June 27th, 2005, 11:54 PM
Authorized User
 
Join Date: Jan 2005
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to JelfMaria
Default

ex.Message and ex.InnerException while giving the same result whats the difference between the two?
Plz answer.
can u give a detailed code to show the differnce btw the 2?



Thanks
Jelfy
 
Old June 28th, 2005, 01:15 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Of course. That's because I created a new Exception based on the Message of the old one.

You can wrap an Exception into another (your own custom exception for example) if you want to hide details, or want to provide more information. E.g.:

try
{

}
catch (SomeDatabaseException ex)
{
  // hiding the details of ex, supplying a new message
  throw new MyApplicationException("Database not available", ex);
}

alternatively, you could decide not to include the original error:

catch (SomeDatabaseException ex)
{
  // hiding the details of ex, supplying a new message
  throw new MyApplicationException("Database not available");
}


Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.





Similar Threads
Thread Thread Starter Forum Replies Last Post
exceptions Maxxim BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 October 18th, 2008 12:34 PM
When to use Exceptions kcraft Pro PHP 1 January 15th, 2005 02:37 PM
Web Exceptions dotnetprogrammer VS.NET 2002/2003 0 November 3rd, 2004 03:39 AM
throwing exceptions...!? jacob ASP.NET 1.0 and 1.1 Basics 3 October 9th, 2003 03:37 PM
Exceptions and errors nbnelson C# 0 August 5th, 2003 11:24 AM





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