creating a custom exception

H

hharry

hello all,

quick syntax question. i am writing a custom exception class and came
across an example:

public class dataProvException : ApplicationException
{
public dataProvException()
{
//
// TODO: Add constructor logic here
//
}

public dataProvException(string msg) : base(msg)
{
//
// TODO: Add constructor logic here
//

}

}

in the second constructor, what is the purpose of : base(msg) ?
i understand that the base keyword ids used to access members of the
base class ?
 
B

Brian Gideon

hharry said:
hello all,

quick syntax question. i am writing a custom exception class and came
across an example:

public class dataProvException : ApplicationException
{
public dataProvException()
{
//
// TODO: Add constructor logic here
//
}

public dataProvException(string msg) : base(msg)
{
//
// TODO: Add constructor logic here
//

}

}

in the second constructor, what is the purpose of : base(msg) ?
i understand that the base keyword ids used to access members of the
base class ?

That calls a base class constructor. In this case you're passing the
exception message to a base class constructor that has one string
parameter.

Also, it is recommended to avoid the use of ApplicationException. If I
remember correctly there was some discussion on whether or not to
deprecate it, but it doesn't appear that happened in 2.0. The idea was
that SystemException would be used for exceptions thrown by the CLR and
ApplicationException would be used for non-CLR exceptions. The problem
is Microsoft didn't follow their own guideline so what seemed like a
good idea is nearly useless now.

http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx

Brian
 
H

hharry

thanks brian,

i'm testing this out now but if i've got this right:

if i initialize a new dataProvException object like this:
dataProvException myEx = new dataProvException("error text");

then, the constructor on the base class (ApplicationException) will be
called instead of the constructor on the derived class
(dataProvException) ?
 
C

Carl Daniel [VC++ MVP]

hharry said:
thanks brian,

i'm testing this out now but if i've got this right:

if i initialize a new dataProvException object like this:
dataProvException myEx = new dataProvException("error text");

then, the constructor on the base class (ApplicationException) will
be called instead of the constructor on the derived class
(dataProvException) ?

The constructor of the derived class _calls_ the constructor of the base
class. Both will run.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top