473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Anyone understood what InnerExceptions are?

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

Mar 29 '07 #1
7 7248
On 29 Mar 2007 04:24:17 -0700, "muler" <mu*************@gmail.com>
wrote:
>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.
--
http://www.kynosarges.de
Mar 29 '07 #2
What you expect as your InnerException?? What should it write to your
calculation??

Nirosh.

"muler" <mu*************@gmail.comwrote in message
news:11*********************@l77g2000hsb.googlegro ups.com...
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

Mar 29 '07 #3
On Mar 29, 2:30 pm, Chris Nahr <dioge...@kynosarges.dewrote:
On 29 Mar 2007 04:24:17 -0700, "muler" <mulugeta.abe...@gmail.com>
wrote:
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.
--http://www.kynosarges.de
Thanks Nahr!

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

Muler

Mar 29 '07 #4
On 29 Mar 2007 04:24:17 -0700, "muler" <mu*************@gmail.com>
wrote:
>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.

--
Philip Daniels
Mar 29 '07 #5
On 29 Mar 2007 04:24:17 -0700, "muler" <mu*************@gmail.com>
wrote:
>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
Mar 29 '07 #6
<Ph***********@foo.comwrote in message
news:h5********************************@4ax.com...
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... ;-)
Mar 29 '07 #7
On Thu, 29 Mar 2007 14:37:59 +0100, "Mark Rae"
<ma**@markNOSPAMrae.comwrote:
><Ph***********@foo.comwrote in message
news:h5********************************@4ax.com.. .
>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... ;-)
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

Mar 29 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

162
by: Isaac Grover | last post by:
Hi everyone, Just out of curiosity I recently pointed one of my hand-typed pages at the W3 Validator, and my hand-typed code was just ripped to shreds. Then I pointed some major sites...
4
by: Stephanie Stowe | last post by:
You have a page. On top of the page, there are some search criteria and other input elements and a submit button. The submit button is clicked, sending a request to the server. Server-side code is...
5
by: Simon Egginton | last post by:
Does anyone know how to make a CHAT ROOM for a webpage using java or jave script please. I am just look at you lot because i think you are the experts! THANK YOU! ewhost_simon"HI"@yahoo.co.uk...
1
by: Prafull Soni | last post by:
hi Can anyone give me program to find difference between two dates? thanks Prafull Soni
33
by: Larry | last post by:
Does anyone use the 3rd party utility CodeRush for VStudio? If so then I would like to see how well it is loved or hated. I have been using the trial for a week and I have a mixed opinion about...
33
by: O-('' Q) | last post by:
....to translate the following to C#.NET style? var i: Integer; begin txtMyIP.Lines.Clear; sHTML := HTTP.Get('http://www.network-tools.com/'); if sHTML = '' then Exit; sIpAddr := 'IP...
5
by: Terry Olsen | last post by:
How can I walk through the InnerExceptions? Would the following code be correct? Private Sub ShowException(ByVal ex As Exception) MsgBox(ex.Message) If ex.InnerException Is Nothing = False Then...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
19
by: William Gill | last post by:
I seem to be having a mentally bad period lately . My code is beginning to be terrible convoluted mess, and I shudder to think what it will be like to go back in a couple months and try to follow...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.