C # question about custom exceptions during application development ,. net provides us with a rigorous Exception capture method to enable the application to run robust. "If a custom Exception inherits from Exception, the disadvantage is that if an Exception inherits from Exception directly, our code may run out of a new exception type that the application knows. This is likely to cause application interruption due to an unprocessed exception. This kind of behavior can easily happen, thinking that we have violated an implicit assumption and that the application has provided any remedy. If we capture this new exception, then ignore it and continue executing the application, it may also produce unexpected results. "The above is taken from Jeffrey Richter's Microsoft. NET. the Framework Program Design provides code implementation // --------------------------------------------------------------------------------- // File: DiskFullException. cs // Description: custom exception // History: // 06/24/2011 // ------------------------------------------------------------------------------------------- using System; using System. IO; using System. runtime. serialization; using System. runtime. serialization. formatters. Soap; // allows DiskFullExceptio instances to instantiate [Serializable] sealed class DiskFullException: Exception, ISerializable {// three common constructors public DiskFullException (): base () // call the constructors of the base class {} public DiskFullException (string message): base (message) // call the constructors of the base class {} public DiskFullException (string message, Exception innerException): base (message, innerException) // call the constructors of the base class {} // define a private FIELD private string diskpath; // a read-only attribute at the top, the newly defined field public strin is returned for modifying the attribute. G DiskPath {get {return diskpath ;}// re-share the property Message, including the newly defined field in the abnormal information text public override string Message {get {string msg = base. message; if (diskpath! = Null) msg + = Environment. newLine + "Disk Path:" + diskpath; return msg ;}// because at least new fields are defined, therefore, we need to define a special constructor for deserialization // because this class is a sealed class, the access restriction of this constructor is defined as a private mode. Otherwise, the constructor // is defined as the protected private DiskFullException (SerializationInfo, StreamingContext context): base (info, context) // Let the base class deserialize the field defined in It {diskpath = info. getString ("DiskPath");} // define the serialization method void ISerializable because at least one field is defined. getObjectData (SerializationInfo info, StreamingContext context) {// serialize each newly defined field info. addValue ("DiskPath", diskpath); // Let the base class serialize the field base defined in it. getObjectData (info, context);} // defines the additional constructor to set the newly defined field public DiskFullException (string message, string diskpath): this (message) // call another constructor {this. diskpath = diskpath;} public DiskFullException (string message, string diskpath, Exception innerException): this (message, innerException) {this. diskpath = diskpath ;}/// the following code tests the serialization class App {static void Main () {// construct a DiskFullException object, serialize DiskFullException e = new DiskFullException ("The diks volume is full", @ "c: \"); FileStream fs = new FileStream (@ "Test", FileMode. create); IFormatter f = new SoapFormatter (); f. serialize (fs, e); fs. close (); // deserialize the DiskFullException object and view its field fs = new FileStream (@ "Test", FileMode. open); e = (DiskFullException) f. deserialize (fs); fs. close (); Console. writeLine ("Type: {1} {0} DiskPath: {2} {0} Message: {3}", Environment. newLine, e. getType (), e. diskPath, e. message); Console. read ();}}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service