Anyone understood what InnerExceptions are?

M

muler

Hi all,

Why am I getting a null value to the InnerException object?

namespace InnerException
{
class D
{
public static int Divide(int i, int j)
{
if (j == 0)
throw new DivideByZeroException("Division by zero is
forbidden.");

return i / j;
}
}

class Program
{
static void Main(string[] args)
{
try
{

int i = int.Parse (Console.ReadLine());
int j = int.Parse(Console.ReadLine()); // enter a 0
value

Console.WriteLine(D.Divide (i , j));
}
catch (Exception ex)
{
if (ex.InnerException != null)
Console.WriteLine("Inner Exception: " +
ex.InnerException.ToString());
else
Console.WriteLine(ex.Message);
}
}
}
}

sample I/O
========
2
0
Division by zero is forbidden.

Can anyone elaborate using examples?

Thanks in advance,
Muler
 
C

Champika Nirosh

What you expect as your InnerException?? What should it write to your
calculation??

Nirosh.
 
C

Chris Nahr

Why am I getting a null value to the InnerException object?

Because there is no inner exception. This property is null by
default. It's only set when an exception handler along the way throws
a _new_ exception that wraps the original exception.
 
M

muler

Because there is no inner exception. This property is null by
default. It's only set when an exception handler along the way throws
a _new_ exception that wraps the original exception.
--http://www.kynosarges.de

Thanks Nahr!

It's solved. All exception class constructors have a parameter to
specify the Inner Exception.

Muler
 
P

PhilipDaniels

Hi all,

Why am I getting a null value to the InnerException object? [snip]

Thanks in advance,
Muler

The InnerException is mainly used by methods that catch an exception,
then rethrow another exception with a better message. In order to
avoid losing all the information from the original exception, it is
set to the InnerException (the exception constructors take an
exception object which will set the InnerException).

It's a very handy technique if you are writing a reusable component.
You can present a problem in the language your users will understand,
but you can also send along the ultimate cause of the problem.

For example:


void InvadeRussia() {
try {
InvadeByeloRussia();
InvadeStalingrad();
} catch (InvasionException ex) {
throw new InvasionException("Unable to invade Russia - try again
next year", ex);
}
}

void ConquerEasternEurope() {
try {
InvadePoland(); // completes successfully
InvadeRussia();
} catch (InvasionException ex) {
// ex.InnerException.Message will be "6th Army surrounded"
}
}

Assume InvadeStalingrad() throws an exception "6th Army surrounded",
this will then be available as the InnerException as shown above.


ps. The .Net framework does not come with an InvasionException, you
will need to construct your own.
 
R

rossum

Hi all,

Why am I getting a null value to the InnerException object?

Because you haven't set it to anything, so it still has the default
value of null. You need to set it explicitly when you catch and
rethrow.
Can anyone elaborate using examples?

Yes:

static void ThrowSomething() {
throw new Exception("This is the first exception.");
}

static void CatchAndRethrow() {
try {
ThrowSomething();
}
catch (Exception ex) {
// Second parameter is the inner exception
// It preserves details of the initial error.
throw new Exception("This is the rethrown exception.", ex);
}
}

static void Main() {
try {
CatchAndRethrow();
}
catch (Exception ex) {
if (ex.InnerException != null)
Console.WriteLine("Inner Exception: " +
ex.InnerException.ToString());
else
Console.WriteLine(ex.Message);
}
Console.ReadLine();
} // end Main()


rossum
 
M

Mark Rae

ps. The .Net framework does not come with an InvasionException, you
will need to construct your own.

It does in v3...

In fact, the entire SystemWar namespace has been seriously upgraded in C#
v3... E.g. the InvadeRussia() method now has two overloads where you can
specify the coldness of the Russian winter and/or the strength of the local
vodka etc...

Also, the InvadeBritain() method has now been removed entirely, as the only
way that ever worked involved setting the system clock back to the year
1066 - setting it any later than that and the InvadeBritain() method simply
didn't work at all... ;-)
 
R

rossum

It does in v3...

In fact, the entire SystemWar namespace has been seriously upgraded in C#
v3... E.g. the InvadeRussia() method now has two overloads where you can
specify the coldness of the Russian winter and/or the strength of the local
vodka etc...

Also, the InvadeBritain() method has now been removed entirely, as the only
way that ever worked involved setting the system clock back to the year
1066 - setting it any later than that and the InvadeBritain() method simply
didn't work at all... ;-)
InvadeBritain() works perfectly well, as long as you use the
InvadeBritain(From right, To left) overload. The InvadeBritain(From
left, To right) overload does indeed fail.

References: ISBN-10: 0413775275, ISBN-13: 978-0413775276

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top