13.3 Examining System.Exception


13.3 Examining System.Exception

Since System.Exception is an important class, I shall examine some of its methods and properties [10] that all other C# exception classes will inherit. Table 13.2 shows the public properties [11] of this class.

[10] If you are unsure about C# properties, just treat them as public class attributes which you can access. C# properties are different from C# fields. C# properties are not class attributes, but implemented like methods. See Chapter 20 for more information.

[11] Besides the public properties listed, there is one int property of protected accessibility called HResult . HResult represents the HRESULT value of an exception. HRESULT is a 32-bit integer with coded information on the severity (information, warning, or error-type exception), the facility (which part of the system is responsible for this exception), and a unique error code. Each exception has a unique HRESULT value. You will not be dealing with the HRESULT value most of the time.

Table 13.2. System.Exception public properties

Public property (type)

Comments

StackTrace (string)

StackTrace carries a stack trace that shows where the exception occurred. Similar to Java's Throwable.printStackTrace() method.

InnerException (exception)

Some exceptions may encapsulate another exception object (the inner exception). InnerException returns this encapsulated exception, or null if there is no inner exception. Inner exceptions will be discussed later.

Message (string)

Textual description of the exception. Similar to Java's Throwable.getMessage() method.

HelpLink (string)

HelpLink is a string that can contain a URL to a help file which provides information on why the exception occurred. For example, it may contain the value "file:///c:/error.htm#err99".

Source (string)

Name of the object or application which caused the exception. If not explicitly set, Source defaults to the name of the .NET assembly (exe or dll file) from which the exception originated.

TargetSite (MethodBase)

The MethodBase object that threw the exception. The MethodBase class encapsulates a method and is commonly used in reflection.

Take particular notice of StackTrace , Message , and InnerException . These three public properties are used frequently. The values of these properties can be specified in the constructor when creating a new instance of the exception. Make use of these properties as if they are public fields of the exception object. Here is an example.

 1: using System;  2:  3: class MyException:ApplicationException{  4:   public MyException(string msg):base(msg){}  5: }  6:  7: class TestClass{  8:   public static void Main(){  9:     try{ 10:       TestException(); 11:     } 12:     catch (Exception e){ 13:       Console.WriteLine("Message       :"+  e.Message  ); 14:       Console.WriteLine("StackTrace    :"+  e.StackTrace  ); 15:       Console.WriteLine("HelpLink      :"+  e.HelpLink  ); 16:       Console.WriteLine("TargetSite    :"+  e.TargetSite  ); 17:       Console.WriteLine("InnerException:"+  e.InnerException  ); 18:       Console.WriteLine("Source        :"+  e.Source  ); 19:    } 20:  } 21: 22:  static void TestException(){ 23:      MyException me = new MyException("help me!"); 24:      me.HelpLink = "http://helpme.com/help"; 25:      throw me; 26:  } 27: } 

Output:

 c:\expt>test Message      :help me! StackTrace   :   at TestClass.TestException()    at TestClass.Main() HelpLink     :http://helpme.com/help TargetSite   :Void TestException() InnerException: Source       :test 

Lines 3 “ 5 contains the code for a user -defined exception class which extends System.ApplicationException . MyException has one constructor which takes in a string (the message description of the exception) and passes the string to its superclass's constructor (using the base keyword) which also takes in a string.

The output shows the InnerException property to be null (nothing is printed out). In this case, Source has the value test because I saved this source file as Test.cs , and test.exe is the assembly which I executed. Source will show the name of the application which was being executed when the exception occurred.

System.Exception has three [12] important overloaded constructors. These are shown in Table 13.3.

[12] This class has four overloaded constructors altogether. The last one enables you to create an instance of the exception class with serialized data.

Table 13.3. Overloaded constructors of System.Exception

Constructor

Comments

public Exception (string)

The passed-in string is the error message that is retrievable via the exception object's Message property.

public Exception()

The default constructor. The exception object is created with a default error message. The default error message goes like this: " Exception of type <type> was thrown ", where <type> is the exception's class name.

public Exception (string, Exception)

Takes in a specified error message, and another Exception instance (the inner exception object).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net