473,800 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ExceptionTestCl ass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticExce ption e)
{
Console.WriteLi ne("ArithmeticE xception Handler: {0}",
e.ToString());
}
catch (Exception e)
{
Console.WriteLi ne("Generic Exception Handler: {0}",
e.ToString());
}
}
}

1. Is this class 'ExceptionTestC lass 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 'ArithmeticExce ption' 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 'ArithmeticExce ption 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 1304
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***@catamara nco.com> wrote in message
news:OZ******** ******@TK2MSFTN GP15.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 ExceptionTestCl ass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticExce ption e)
{
Console.WriteLi ne("ArithmeticE xception Handler: {0}",
e.ToString());
}
catch (Exception e)
{
Console.WriteLi ne("Generic Exception Handler: {0}",
e.ToString());
}
}
}

1. Is this class 'ExceptionTestC lass 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 'ArithmeticExce ption' 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 'ArithmeticExce ption 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*****@TAKETH ISOUTkamath.com > wrote in message
news:Os******** ******@TK2MSFTN GP15.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***@catamara nco.com> wrote in message
news:OZ******** ******@TK2MSFTN GP15.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 ExceptionTestCl ass
{
public static void Main()
{
int x = 0;
try
{
int y = 100/x;
}
catch (ArithmeticExce ption e)
{
Console.WriteLi ne("ArithmeticE xception Handler: {0}",
e.ToString());
}
catch (Exception e)
{
Console.WriteLi ne("Generic Exception Handler: {0}",
e.ToString());
}
}
}

1. Is this class 'ExceptionTestC lass 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 'ArithmeticExce ption' 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 'ArithmeticExce ption 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
2571
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 handy for that purpose. The only problem when dealing with f.p. exception signals is that there is (afaik) no specification *when* the f.p. exception is raised, with one notable exception: 'feraiseexcept(int)' raises the exceptions passed in the...
9
5005
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 following code exhibits the bug. If it is built and run from Visual Studio.NET 2003 in debug mode it works correctly as expected. However, if built in Release mode it fails with an "uncaught exception" error, although the exception does have a valid...
44
4233
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
40
13541
by: Kevin Yu | last post by:
is it a bad programming design to throw exception in the try block then catch it??
2
3886
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 have a mapping of .* to the aspnet_isapi.dll. And my web.config has an entry thusly: <add verb="*" path="*" type="HandlerClass, HandlerAssembly" /> And this is working just fine thus far. The handler gets all the requests and works like a...
2
1325
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 to disable a dialog of my liking. Now if I run the application outside of the IDE I recieve the standard "Unhandled exception" dialog provided by .NET giving me the ability to continue or quit. I wasn't actually aware that exception handling...
11
5602
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
5066
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? Do I define a default error handler as well as do my try/catch? Can I mix error handling with the exceptions? Is one approach better than the other? TIA John
6
4400
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 restart my application, sometimes several times a day The customer has XP Home SP2
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10504
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10274
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.