Exceptionargs. CS:
// Exception information base class <br/> [serializable] <br/> public abstract class exceptionargs {</P> <p> Public Virtual string message {<br/> get {<br/> return string. empty; <br/>}< br/>}
Generic exception classes:
[Serializable] <br/> Public sealed class exception <texceptionargs>: exception, system. runtime. serialization. iserializable <br/> where texceptionargs: predictionargs {<br/> private const string c_args = "ARGs"; <br/> private readonly texceptionargs m_args; </P> <p> Public texceptionargs ARGs {<br/> get {<br/> return m_args; <br/>}</P> <p> Public exception (string message = NULL, exception innerex Ception = NULL) <br/>: This (null, message, innerexception) {}</P> <p> Public exception (texceptionargs ARGs, string message = NULL, exception innerexception = NULL) <br/>: Base (message, innerexception) {m_args = ARGs ;}</P> <p> // This constructor is used for deserialization, because the class is sealed, the constructor is private. <br/> // if the class is not sealed, this constructor should be protected <br/> [securitypermission (securityaction. linkdemand, flags = securitypermissionflag. serializationformatter)] <br /> Private exception (serializationinfo info, streamingcontext context) <br/>: Base (Info, context) {<br/> m_args = (texceptionargs) info. getvalue (c_args, typeof (texceptionargs); <br/>}</P> <p> // This method is used for serialization. Because the iserializable interface is implemented, therefore, it is public (the method defined in iserializable) <br/> // It has been implemented in the exception class. This class inherits exception, and overwrites the implementation of this method in exception <br/> [securitypermission (securityaction. linkdemand, flags = securitypermissionflag. s Erializationformatter)] <br/> Public override void getobjectdata (serializationinfo, streamingcontext context) {<br/> info. addvalue (c_args, m_args); <br/> base. getobjectdata (Info, context); <br/>}</P> <p> Public override string message {<br/> get {<br/> string basemsg = base. message; <br/> return (m_args = NULL )? Base. message: basemsg + "(" + m_args.message + ")"; <br/>}</P> <p> Public override bool equals (Object OBJ) {<br/> exception <texceptionargs> Other = OBJ as exception <texceptionargs>; <br/> If (OBJ = NULL) {<br/> return false; <br/>}< br/> return object. referenceequals (m_args, other. m_args) & base. equals (OBJ); <br/>}</P> <p> Public override int gethashcode () {<br/> return base. gethashcode (); <br/>}< br/>}
Note: C # Only allow custom classes to inherit from the base class of the system exception class: system. and inherit from system. all exception types of the exception class must be serializable, so that the exception information can be crossed through the appdomain (for example, the remoting server may need to return exceptions to the remote caller, in this case, you have to traverse the appdomain) or write logs/databases.
Define an exception class when the disk is full:
// Define an exception class when the disk is full <br/> [serializable] <br/> Public sealed class diskfullexceptionargs: exceptionargs {<br/> // readonly: Read-only, dynamic constant, the value can only be assigned to the constructor <br/> private readonly string m_diskpath; </P> <p> Public diskfullexceptionargs (string diskpath) {<br/> m_diskpath = diskpath; <br/>}</P> <p> Public String diskpath {<br/> get {<br/> return m_diskpath; <br/>}</P> <p> Public override string message {<br/> G Et {<br/> return (m_diskpath = NULL )? Base. message: "diskpath =" + m_diskpath; <br/>}</P> <p>}
If no additional data is included in the class, you can simply write:
// Define an exception class when the disk is full <br/> [serializable] <br/> Public sealed class diskfullexceptionargs: exceptionargs {}
Throw/catch custom exceptions:
Public void testexception () {<br/> try {<br/> throw new exception <diskfullexceptionargs> (<br/> New diskfullexceptionargs (@ "C :/"), "The disk is full"); <br/>}< br/> catch (exception <diskfullexceptionargs> ex) {<br/> console. writeline (ex. message); <br/>}< br/>}