473,401 Members | 2,127 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,401 software developers and data experts.

Exception handler in sdk


I would appreciate some help in understanding the simple C# example relating
to handling exceptions. This one relates to catching an error thrown by
dividing number by zero.

There are a few things I don't understand which I hope you can help me with:


'using System;

class ExceptionTestClass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticException e)
{
Console.WriteLine("ArithmeticException Handler: {0}",
e.ToString());
}
catch (Exception e)
{
Console.WriteLine("Generic Exception Handler: {0}",
e.ToString());
}
}
}

1. Is this class 'ExceptionTestClass a 'test' class for demonstration
purposes...why is there is not a general Exception class one could call
rather than the author's test?

2. The variable 'ArithmeticException' and 'e' ...are these standard incoming
variables from the excpection class or the author's exception class?

3. Why does he have two 'catch' classes....does the second one check the
first one to see if there was an error....

4. What is the difference between the 'ArithmeticException Handler and the
'Generic Exception Handler'.
I realise these are proabably naive questions but I am just starting out.

Thanks
Jason

Nov 19 '05 #1
2 1290
Jason,

1. Looks like a test class -- for demo purposes. It is a general practice to
catch exceptions at the top-most level class -- say UI. However, te
intentions here might be different (catch an exception, so the UI does not
have to)

2. The format is catch (<exception class> var)

The var is there so you can use it..

catch (Exception e)
{
// Show what happened
MessageBox.Show(e.ToString());
}

If you don't plan to use the exception object, just don't declare it

catch (Exception)
{
// do something generic here
}

for #3 and #4 see below:

Exception is a base class for all exceptions -- other classes derive from
that. So, if you want to catch just about any exception, all you have to do
is:

catch (Exception e)

However, if you want to catch specific exceptions, you have to do it before
you catch the general, broader, exception.
So, let us say:

Exception <--- MyExp1 <-- MyExp2

Where <-- is the inheritance tree.

Then, your catch sequence should go from MyExp2 to Exception
catch (MyExp2 e1)
{
}

catch (MyExp1 e2)
{
}

catch (Exception e)
{
}

The catch (Exception e) is the "catch all"

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
<ja***@catamaranco.com> wrote in message
news:OZ**************@TK2MSFTNGP15.phx.gbl...

I would appreciate some help in understanding the simple C# example relating to handling exceptions. This one relates to catching an error thrown by
dividing number by zero.

There are a few things I don't understand which I hope you can help me with:

'using System;

class ExceptionTestClass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticException e)
{
Console.WriteLine("ArithmeticException Handler: {0}",
e.ToString());
}
catch (Exception e)
{
Console.WriteLine("Generic Exception Handler: {0}",
e.ToString());
}
}
}

1. Is this class 'ExceptionTestClass a 'test' class for demonstration
purposes...why is there is not a general Exception class one could call
rather than the author's test?

2. The variable 'ArithmeticException' and 'e' ...are these standard incoming variables from the excpection class or the author's exception class?

3. Why does he have two 'catch' classes....does the second one check the
first one to see if there was an error....

4. What is the difference between the 'ArithmeticException Handler and the
'Generic Exception Handler'.
I realise these are proabably naive questions but I am just starting out.

Thanks
Jason

Nov 19 '05 #2
Many thanks Manohar,

That makes it a lot clearer.

- Jason
"Manohar Kamath" <mk*****@TAKETHISOUTkamath.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
Jason,

1. Looks like a test class -- for demo purposes. It is a general practice to catch exceptions at the top-most level class -- say UI. However, te
intentions here might be different (catch an exception, so the UI does not
have to)

2. The format is catch (<exception class> var)

The var is there so you can use it..

catch (Exception e)
{
// Show what happened
MessageBox.Show(e.ToString());
}

If you don't plan to use the exception object, just don't declare it

catch (Exception)
{
// do something generic here
}

for #3 and #4 see below:

Exception is a base class for all exceptions -- other classes derive from
that. So, if you want to catch just about any exception, all you have to do is:

catch (Exception e)

However, if you want to catch specific exceptions, you have to do it before you catch the general, broader, exception.
So, let us say:

Exception <--- MyExp1 <-- MyExp2

Where <-- is the inheritance tree.

Then, your catch sequence should go from MyExp2 to Exception
catch (MyExp2 e1)
{
}

catch (MyExp1 e2)
{
}

catch (Exception e)
{
}

The catch (Exception e) is the "catch all"

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
<ja***@catamaranco.com> wrote in message
news:OZ**************@TK2MSFTNGP15.phx.gbl...

I would appreciate some help in understanding the simple C# example

relating
to handling exceptions. This one relates to catching an error thrown by
dividing number by zero.

There are a few things I don't understand which I hope you can help me

with:


'using System;

class ExceptionTestClass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticException e)
{
Console.WriteLine("ArithmeticException Handler: {0}",
e.ToString());
}
catch (Exception e)
{
Console.WriteLine("Generic Exception Handler: {0}",
e.ToString());
}
}
}

1. Is this class 'ExceptionTestClass a 'test' class for demonstration
purposes...why is there is not a general Exception class one could call
rather than the author's test?

2. The variable 'ArithmeticException' and 'e' ...are these standard

incoming
variables from the excpection class or the author's exception class?

3. Why does he have two 'catch' classes....does the second one check the
first one to see if there was an error....

4. What is the difference between the 'ArithmeticException Handler and the 'Generic Exception Handler'.
I realise these are proabably naive questions but I am just starting out.
Thanks
Jason


Nov 19 '05 #3

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

Similar topics

5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
9
by: David B | last post by:
Why is it so difficult to report bugs to Microsoft? I have a documented bug and an small test example. I don't really see why I should have to pay to tell them about it... Anyway, the...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
40
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
2
by: Seth | last post by:
Ok, here is my setup. I have a fully functioning HTTP Handler implemented. The handler is supposed to handle every single request that comes in to a particular virtual directory. Thus, in IIS, I...
2
by: Nak | last post by:
Hi there, I'm just curious as to something. I have just added an exception handler at the entry point to my application, within the IDE any unhandled exceptions fallback to this and enable me...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
16
by: john6630 | last post by:
Coming from the .Net world, I am used to the try...catch...finally approach to error handling. And PHP 5 now supports this approach. But I am not clear what happens to unhandled errors/exceptioins?...
6
by: Steve | last post by:
Hi All I have a windows forms Application (SAM) in vb.net 2008 using .net framework V2 One and only one customer out of 30 customers is getting errors daily where they have to close and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.