Hi i just read:
http://msdn.microsoft.com/library/de...ceptdotnet.asp
Wich is interesting reading by the way.
But. I have'nt used exception very much to anything else than ordinary
FileExeptions etc. Now i want to create my own exception hierarchy.
I want to do this so that i can log the exceltion information to a file or a
database. Doesent mather right now.
But I can't Figure out where to put the loggin rutine after reading the
article. I suggest that i make a baseexception, wich i Iseriazable
compliant. I have done that. But is it in my Baseclass exception that i
sjould implement my loggin routine?
Or is it in my derrived Custom Exception? Or should the logging rutine be in
a seperate class wich i call when i Catch one of my Cusotm Exceptions?
I have made this so far, but stopped because i have doubt that it s the way
to go.
THX in reagards if someone can clear out for me how to create custom
exceptions wich can be logged. (See sample code i have made)
Anders, Denmark
(Some Sample code)
========BASECLA SSEXCEPTION==== ======
[Serializable]
public class TurabsCriticalE xception : ApplicationExce ption
{
Exception t;
public TurabsCriticalE xception() : base()
{
saveExceptionTo File();
}
public TurabsCriticalE xception(string message) : base(message)
{
MessageInternal = message;
saveExceptionTo File();
}
public TurabsCriticalE xception(string message, Exception inner) :
base(message, inner)
{
t = inner;
MessageInternal = message;
saveExceptionTo File();
}
protected TurabsCriticalE xception(Serial izationInfo info, StreamingContex t
context) : base(info,conte xt)
{
m_strMachineNam e = info.GetString( "m_strMachineNa me");
}
public override void GetObjectData(S erializationInf o info, StreamingContex t
context)
{
info.AddValue(" m_strMachineNam e", m_strMachineNam e, typeof(String)) ;
info.AddValue(" m_DateAndTIme", m_DateAndTime,t ypeof(DateTime) );
base.GetObjectD ata (info, context);
}
private string m_strMachineNam e = Environment.Mac hineName;
public string MachineName
{
get{return m_strMachineNam e;}
set{m_strMachin eName = value;}
}
private string m_MessageIntern al = string.Empty;
public string MessageInternal
{get{return m_MessageIntern al;}
set{m_MessageIn ternal = value;}
}
private DateTime m_DateAndTime = DateTime.Now;
public DateTime DateAndTime
{get{return m_DateAndTime;}
set{m_DateAndTi me = value;}
}
protected void saveExceptionTo File()
{
try
{
FileStream fs = new FileStream(@"c: \Exception.txt" , FileMode.OpenOr Create,
FileAccess.Writ e);
StreamWriter m_streamWriter = new StreamWriter(fs );
m_streamWriter. BaseStream.Seek (0, SeekOrigin.End) ;
m_streamWriter. WriteLine("==== = EXCEPTION LOG FILE ======");
m_streamWriter. WriteLine(Machi neName.ToString ());
m_streamWriter. WriteLine(DateA ndTime.ToShortT imeString());
m_streamWriter. Flush();
fs.Close();
}
catch(Exception ex)
{
string e = ex.Message;
//Write to the System log instead
}
finally
{
}
}
========DERRIVE DCLASSEXCEPTION ==========
public class TurabsCriticalD atabaseExceptio n : TurabsCriticalE xception
{
public TurabsCriticalD atabaseExceptio n(string message) : base(message)
{
}
}
========TESTCLA SS==========
public class DB
{
}
public int DbStuff()
{
throw new TurabsException s.TurabsCritica lDatabaseExcept ion("Server having
problem!");
}
}
========TESTCOD E==========
DB db = new DB();
try
{
db.DbStuff(6);
}
catch(TurabsCri ticalException ex)
{
// EXCEPTION HAS BEEN WRITTEN TO FILE
}