Chapter 5: Exception Handling and Object Lifetime lecture in color contents of chapter5.txt:

Chapter 5: Exceptions and Object Lifetime

Two goals:

  1. How to handle run time anomalies in your code through the use of structured exception handling.
  2. Understand and control the process of object lifetime management by the CLR.

Problems with Code come in three flavors: Bugs, Errors, and Exceptions.

History of Exception Handling:

The .NET Exception Handling contains for parts:

  1. A type that represents the details of the exceptional circumstance
  2. A method that throws the exception to the caller
  3. A block of code that will invoke the exception-ready method.
  4. A block of code that will process (or catch) the exception (if it occurs)

Throwing an exception:

Public void Speedup(int delta){
       if(carIsDead) throw new Exception(“This car is already dead…”);
       else{}
}

Catching Exceptions:

I=============================================================================================================================

I=============================================================================================================================

Object Lifetime

Finalizing (explicit memory deallocation for unmanaged resources through System.Object.Finalize())

               Class FinalizedCar{
                       ~FinalizedCar(){Console.WriteLine(“=> Finalizing car…”); }
               }

The IDisposable Interface

       public Car : IDisposable{
               // custom method that the user must call manually
               public void Dispose(){
               // Clean up the internal unmanaged resources here
               }
       }
       public static int Main(string[] args){
               car c1 = new Car(“F1”, 400, 10);
               c1.Dispose();
               return 0;
       }// c1 is still on the heap and maybe collected at this point
       

Garbage Collection Optimizations

Forcing a Garbage Collection


lecture in color

/comp/194NET/notes/chapter5.php3
downloaded on Nov-23-2009 05:29:06 AM,
was last modified on Feb-24-2004 08:32:13 AM.

All lecture note content is copyright 2004 by the Tufts .NET Study Group