[ Pobierz całość w formacie PDF ]
.Process();System.Console.WriteLine("Transaction went through");} catch (TransactionFailureException tfe) {System.Console.WriteLine(tfe.Message);System.Console.Error.WriteLine(tfe.InnerException);}}public static void Main(){BusinessLogic bl = new BusinessLogic();for (int i = 0; i < 10; i++) {bl.DoCharge();}}}} ///:~In this example, the class Transaction has an exception class that is at itssame level of abstraction in TransactionFailureException.Thetry…catch(Exception e) construct in Transaction.Process() makes for a nice andexplicit contract: “I try to return void, but if anything goes awry in myprocessing, I may throw a TransactionFailedException.” In order to generatesome exceptions, we use a random number generator to throw different types oflow-level exceptions in Transaction.Process().All exceptions are caught in Transaction.Process()’s catch block, where theyare placed “inside” a new TransactionFailureException using that type’soverridden constructor that takes an exception and creates a generic “Logicalfailure caused by low-level exception” message.The code then throws the newlycreated TransactionFailureException, which is in turn caught byBusinessLogic.DoCharge()’s catch(TransactionFailureException tfe) block.Thehigher-abstraction exception’s message is printed to the Console, while thelower-abstraction exception is sent to the Error stream (which is also theconsole, but the point is that there is a separation between the two levels ofabstraction.In practice, the higher-abstraction exception would be used forbusiness logic choices and the lower-abstraction exception for debugging).Standard C# exceptionsThe C# class Exception describes anything that can be thrown as an exception.There are two general types of Exception objects (“types of” = “inheritedfrom”).SystemException represents exceptions in the System namespace and itsdescendants (in other words,.NET’s standard exceptions).Your exceptions byconvention should extend from ApplicationException.If you browse the.NET documentation, you’ll see that each namespace has asmall handful of exceptions that are at a level of abstraction appropriate tothe namespace.For instance, System.IO has an InternalBufferOverflowException,which is pretty darn low-level, while System.Web.Services.Protocols hasSoapException, which is pretty darn high-level.It’s worth browsing throughthese exceptions once to get a feel for the various exceptions, but you’ll soonsee that there isn’t anything special between one exception and the next exceptfor the name.The basic idea is that the name of the exception represents theproblem that occurred, and the exception name is intended to be relativelyself-explanatory.Performing cleanupwith finallyThere’s often some piece of code that you want to execute whether or not anexception is thrown within a try block.This usually pertains to some operationother than memory recovery (since that’s taken care of by the garbagecollector).To achieve this effect, you use a finally clause at the end of allthe exception handlers.The full picture of an exception handling section isthus:try {// The guarded region: Dangerous activities// that might throw A, B, or C} catch(A a1) {// Handler for situation A} catch(B b1) {// Handler for situation B} catch(C c1) {// Handler for situation C} finally {// Activities that happen every time}In finally blocks, you can use control flow statements break, continue, or gotoonly for loops that are entirely inside the finally block; you cannot perform ajump out of the finally block.Similarly, you can not use return in a finallyblock.Violating these rules will give a compiler error.To demonstrate that the finally clause always runs, try this program://:c09:AlwaysFinally.cs// The finally clause is always executed [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • luska.pev.pl
  •