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

C# - writing a custom exception class

Hi ,

I'm building an app that consists of a GUI that calls various functions in a DLL that I wrote (and consists of some various classes).

I am wondering how should I implement an Exception Mechanism in this project - Should I use "try" blocks only in the GUI ? (uppermost level)

and plus , if someone would kindly explain me about the "custom exception" class - how should it work exactly ?

Thanks . :-)
Oct 9 '07 #1
4 2636
Plater
7,872 Expert 4TB
I think that .DLL files should let the exceptions pass through up to what's calling it. If the dll were sealed, it would drive you nuts to not be getting the exceptions. (So try/catch blocks in gui only I think)

As for custom exceptions:
Expand|Select|Wrap|Line Numbers
  1. public class myexeption:Exception
  2. {
  3. // ...
  4. }
  5.  
Oct 9 '07 #2
So let me get it straight..should the custom exception class be inside the DLL ? or in the GUI itself ? (the DLL is not sealed).

Plus - I have another problem.
one of my classes in the DLL is reading an XML file.
I want the user to be displayed with a generic "XML Parsing Error" message - nothing more than that.
if in the GUI I have the following code :

try
{
ReadXmlFunction()
}
catch(CustomExceptionClass ex)
{


}

and in the ReadXmlFunction() - an xmlException will be thrown - the catch block won't catch it - why is that ?

The custom exception class that I wrote looks like this :

public class CustomException : Exception
{
enum Type { SqlException, XmlException, Exception,Custom };

Type ExceptionType;

public CustomException(Exception ex):base()
{

switch (ex.GetType().ToString())
{
case "SqlException":
ExceptionType = Type.SqlException;
break;
case "XmlException":
ExceptionType = Type.XmlException;
break;
default:
ExceptionType = Type.Exception;
break;
}
}

public CustomException(string detail):base(detail)
{
ExceptionType = Type.Custom;
}


public string GetErrorDescription()
{
switch (ExceptionType)
{
case Type.SqlException:
return String.Format("DB Error - {0}\nError Serial # : {1}", ((SqlException)(this.GetBaseException())).Message, ((SqlException)(this.GetBaseException())).Number);

case Type.XmlException:
return String.Format("XML Parsing Error - {0}", ((System.Xml.XmlException)(this.GetBaseException() )).Message);

case Type.Exception:
return String.Format("Unrecognized General Error - {0}", Message);

case Type.Custom:
return String.Format("Reporting System Error - {0}", Message);
}
return "Critical Error recognition error. Error is : " + Message;
}

}




Thanks in advance.
Oct 9 '07 #3
Plater
7,872 Expert 4TB
It would be a rather nice looking package if you created your custom exception class(es, plural? if needed) and put it in the dll where you used it.
Then you could try/catch INSIDE your dll, and when you catch an exception, throw an instance of your custom exception with the amount of information you want exposed.
Then your GUI (or anything else that uses your DLL) will be catching your custom exceptions.
Oct 9 '07 #4
It would be a rather nice looking package if you created your custom exception class(es, plural? if needed) and put it in the dll where you used it.
Then you could try/catch INSIDE your dll, and when you catch an exception, throw an instance of your custom exception with the amount of information you want exposed.
Then your GUI (or anything else that uses your DLL) will be catching your custom exceptions.
Great Idea - Thanks a lot :-)
Oct 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Flare | last post by:
Hi i just read: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/exceptdotnet.asp Wich is interesting reading by the way. But. I have'nt used exception very much to...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
6
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ...
3
by: Dan | last post by:
Hi all! When I throw my custom Exception class the first time in my code, the compiler takes a lot of time for find the following catch EX: try Throw New MyCustomException("test")
1
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database -...
3
by: Tomsawyer2kin_hotmail | last post by:
Hi, I created a custom Exception Class as Public Class CSTException : Inherits System.ApplicationException ' required to over ride the base class method Sub New(ByVal ExceptionCode As...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
5
by: Alias | last post by:
Hi - I'm trying to implement a custom RoleProvider based on the SqlRoleProvider. I keep receiving a an error that it can't load type 'MyRoleTest.MyRoleProvider' when trying to load my...
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
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
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...
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.