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

Derive custom exception types

Could someone show me how to derive and use custom exception types
separately from:System.Exception, System.Object and
System.ApplicationException in C#?

When people say:"Use Visual Studio .NET to create a component..." What does
it mean by "component" here in C#?

Nov 15 '05 #1
5 6731
You should create your own class, hat derives from System.SystemException
and override the appropriate members.

--
Telmo Sampaio
MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT,
MSF Practitioner, ITIL Certified
te***********@hotmail.com
"p988" <p9**@hotmail.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl...
Could someone show me how to derive and use custom exception types
separately from:System.Exception, System.Object and
System.ApplicationException in C#?

When people say:"Use Visual Studio .NET to create a component..." What does it mean by "component" here in C#?


Nov 15 '05 #2
Here is a working example (console application) that deomonstrates what you
want to know...

/* Copyright 2003 O'Reilly - used with permission from source code for
Programming C# 3rd Edition by Jesse Liberty */

namespace Programming_CSharp
{
using System;

public class MyCustomException :
System.ApplicationException
{
public MyCustomException(string message):
base(message)
{

}
}

public class Test
{
public static void Main( )
{
Test t = new Test( );
t.TestFunc( );
}

// try to divide two numbers
// handle possible exceptions
public void TestFunc( )
{
try
{
Console.WriteLine("Open file here");
double a = 0;
double b = 5;
Console.WriteLine ("{0} / {1} = {2}",
a, b, DoDivide(a,b));
Console.WriteLine (
"This line may or may not print");
}

// most derived exception type first
catch (System.DivideByZeroException e)
{
Console.WriteLine(
"\nDivideByZeroException! Msg: {0}",
e.Message);
Console.WriteLine(
"\nHelpLink: {0}\n", e.HelpLink);
}
catch (MyCustomException e)
{
Console.WriteLine(
"\nMyCustomException! Msg: {0}",
e.Message);
Console.WriteLine(
"\nHelpLink: {0}\n", e.HelpLink);
}
catch
{
Console.WriteLine(
"Unknown exception caught");
}
finally
{
Console.WriteLine ("Close file here.");
}

}

// do the division if legal
public double DoDivide(double a, double b)
{
if (b == 0)
{
DivideByZeroException e =
new DivideByZeroException( );
e.HelpLink=
"http://www.libertyassociates.com";
throw e;
}
if (a == 0)
{
MyCustomException e =
new MyCustomException(
"Can't have zero divisor");
e.HelpLink =
"http://www.libertyassociates.com/NoZeroDivisor.htm";
throw e;
}
return a/b;
}
}
}


"p988" <p9**@hotmail.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl...
Could someone show me how to derive and use custom exception types
separately from:System.Exception, System.Object and
System.ApplicationException in C#?

Nov 15 '05 #3
When you do this, you should make sure that you create the following
constructors:

1) The usual constructor that you'd use to create the exception.
2) A constructor that takes an inner exception, so that you can use this
exception to wrap other exceptions
3) A serialization constructor (and make the exception serializable). This
allows it to pass across through remoting.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Telmo Sampaio" <te***********@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
You should create your own class, hat derives from System.SystemException
and override the appropriate members.

--
Telmo Sampaio
MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT,
MSF Practitioner, ITIL Certified
te***********@hotmail.com
"p988" <p9**@hotmail.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl...
Could someone show me how to derive and use custom exception types
separately from:System.Exception, System.Object and
System.ApplicationException in C#?

When people say:"Use Visual Studio .NET to create a component..." What

does
it mean by "component" here in C#?



Nov 15 '05 #4
Thanks for adding that Eric!

--
Telmo Sampaio
MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT,
MSF Practitioner, ITIL Certified
te***********@hotmail.com
"Eric Gunnerson [MS]" <er****@online.microsoft.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
When you do this, you should make sure that you create the following
constructors:

1) The usual constructor that you'd use to create the exception.
2) A constructor that takes an inner exception, so that you can use this
exception to wrap other exceptions
3) A serialization constructor (and make the exception serializable). This
allows it to pass across through remoting.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "Telmo Sampaio" <te***********@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
You should create your own class, hat derives from System.SystemException and override the appropriate members.

--
Telmo Sampaio
MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT, MSF Practitioner, ITIL Certified
te***********@hotmail.com
"p988" <p9**@hotmail.com> wrote in message
news:uc**************@TK2MSFTNGP11.phx.gbl...
Could someone show me how to derive and use custom exception types
separately from:System.Exception, System.Object and
System.ApplicationException in C#?

When people say:"Use Visual Studio .NET to create a component..."
What does
it mean by "component" here in C#?




Nov 15 '05 #5

"Telmo Sampaio" <te***********@hotmail.com> wrote in message
news:e0**************@TK2MSFTNGP11.phx.gbl...
You should create your own class, hat derives from System.SystemException
and override the appropriate members.


Err no, you should really derive from ApplicationException. This class is
used to differentiate between exceptions thrown by an application versus the
system

andrew
Nov 15 '05 #6

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

Similar topics

7
by: Blake T. Garretson | last post by:
I'm having some issues with decimal.Decimal objects playing nice with custom data types. I have my own matrix and rational classes which implement __add__ and __radd__. They know what to do with...
2
by: Shanthi | last post by:
I am using property grid in C# application. Property grid displays the propeties properly. But I have some problem in displaying custom validation message. I have integer type property. When I...
7
by: Julia | last post by:
Hi I have a system which composed from 2 different components 1.MailSender 2.MailReciever the Name of my system is JuliaSystem
5
by: Jon Skeet [C# MVP] | last post by:
I've run against a problem which I'm *sure* must be easy to solve - but I'm blowed if I can find the answer :( I have a web service which I want to require authentication. I need to authenticate...
2
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
1
by: leodippolito | last post by:
Dear sirs, I am using custom wrappers to primitive types in my classes, so I can have some flags when working with the database ("undefined" and "null") .. So instead of: public class...
3
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: ...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
0
by: purplepangolin | last post by:
I have a generic base class and need a list to contain different types of objects that derive from this. Public class genericBase<T> where T: ConfigBase{} I have tried :...
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
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
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,...
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
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...

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.