Exception handling module is a large system must be a component, well-designed exception handling module can improve the robustness of the system. Let's talk about all aspects of exception handling from the perspective I understand. My design is only limited to Windows Forms for reference.
1 Defining exception types
The. NET framework defines a number of exception types, and we add some custom exception types to the ERP system based on actual needs.
Database Access exception: LLBL Gen Pro has defined several common types of exceptions, common types of exceptions, and their role profiles.
Ormconcurrencyexception concurrency exception, entity has been deleted when updating entity, constraint cannot be deleted when deleted
Ormentityoutofsyncexception. (Adapter) entity is saved after it has not been re-read, using its properties when thrown ormentityisdeletedexception entity has been deleted, but still accesses its properties
Ormfieldisnullexception. After the attribute value of an entity is set to NULL, it still accesses its properties
Ormentityvalidationexception Custom Exceptions
Ormfieldisreadonlyexception assigning values to read-only properties
Orminheritanceinfoexception error detected during query execution
Ormqueryconstructionexception ORM Framework constructs dynamic query Engine failure when Ormqueryexecutionexception ORM Framework performs dynamic query Query Engine) fails
Ormrelationexception Relationship Setting Error
Ormsecurityexception for authorization (Authorization) failure
The value of the Ormvaluetypemismatchexception property does not match the type
business Logic Exception: defines the type of business logic exception in the application
Accessdeniedexception module or feature currently logged in user no access
Crystalreportexception Crystal Report runtime failed to load, report connection database failed, report formula parsing failed, and other exceptions
Entityvalidationexception business Object Validation failed
Fieldvalidationexception Property validation failed for a business object
LicenseException License Authorization exception
Ftpexception: Exceptions such as file server connection failure or authorization failure
2 Package Exception information
When the system throws an exception, we need to know the complete information of the program that throws the exception, such as the program version, the last update time, the stack of exceptions, etc., with this information, the technical support or programmer can quickly locate the exception and analyze the possible causes.
For this purpose, define an exception information wrapper class that contains the incoming exception, encapsulating the richer exception information.
Public Sealed classexceptiondetail{PrivateSystem.Exception _exception;Private voidInitialize () {if( This. _exception! =NULL{builder = Builder. Append (format). Replace ("\ n","\ r \ n"); Builder. Append (string. Format ("Date: {0} {1}\r\n", DateTime.Today.ToShortDateString (), DateTime.Now.ToLongTimeString ())); Builder. Append (string. Format ("Version: {0} ({1}) \ r \ n", Assemblyversion.version, File.getlastwritetime (typeof(AssemblyVersion). (assembly.location))); Builder. Append (string. Format ("Source: {0}\r\n", Innerexception.source)); Builder. Append (string. Format ("Class: {0}\r\n", (Innerexception.targetsite! =NULL) ? InnerException.TargetSite.DeclaringType.ToString ():NULL)); Builder. Append (string. Format ("Member Type: {0}\r\n", (Innerexception.targetsite! =NULL) ? InnerException.TargetSite.MemberType.ToString ():NULL)); Builder. Append (string. Format ("Member Name: {0}\r\n", Innerexception.targetsite)); Builder. Append (string. Format ("Exception Type: {0}\r\n", Innerexception.gettype (). FullName)); Builder. Append (string. Format ("Data: {0}\r\n", obj2)); Builder. Append ("\ r \ n"); Builder. Append (string. Format ("Exception: {0}", message)); } }}
3 Catch system throws an exception
For Windows Forms programs, you can complete a capture of system exceptions with two property settings.
New New New Unhandledexceptioneventhandler (currentdomain_unhandledexception);
CustomExceptionHandler is a processing exception information sealed class, the source code is as follows, the purpose is to unify the system's exception error prompt interface.
Internal Sealed classcustomexceptionhandler{ Public BOOLIsdebug =false; PublicCustomExceptionHandler () {}//handle the Exception event Public voidOnThreadException (Objectsender, Threadexceptioneventargs t) {if(Isdebug)
Debug.Assert (false, T.exception.message, T.exception.stacktrace); DialogResult result = DialogResult.Cancel;Try{result = This. Showthreadexceptiondialog (t.exception); }Catch{Try{result = MessageBox.Show (string. Format ("{0}\r\n\r\n{1}", T.exception.message, T.exception.stacktrace),"Fatal Error", MessageBoxButtons.OK, Messageboxicon.error); }finally{application.exit (); } }if(Result = = Dialogresult.abort) Application.exit (); }PrivateDialogResult Showthreadexceptiondialog (Exception e) {returnDialogResult.Cancel; } }
The Exception Display dialog box shows the exception, referring to the following interface.
4 can throw exception through throw statement, implement N-layer rollback
When you save a new entity object, determine whether the data is duplicated:
if (salescontract.isnew) { Isalescontractmanager Salescontractmanager = createproxyinstance<isalescontractmanager> (); if (Salescontractmanager.issalescontractexist (Salescontract.contractno)) Throw New "Cotract No." is already used ");}
Trigger validation when a property change event occurs:
Public Override BOOLValidatefieldvalue (Ientitycore involvedentity,intFieldindex,Object value){BOOLresult =Base. Validatefieldvalue (involvedentity, Fieldindex,value);if(!result)return false;Switch((Salescontractfieldindex) fieldindex) { CaseSalescontractfieldindex.customerno:return This. Validatecustomerno ((string)value); }return true;}Private BOOLValidatecustomerno (string value){if(!string. IsNullOrEmpty (value) {Icustomermanager Customermanager = clientproxyfactory.createproxyinstance<icustomermanager> (); Customermanager.validatecustomerno (Shared.currentusersessionid,value); }return true;}
The core part of Windows forms exception handling in the third section of this article sets the exception that is thrown by the capture system.
Parsing large. NET ERP system Design exception handling module