如果obj
是一個參數的方法,你應該拋出一個ArgumentException
:
throw new ArgumentException("Type " + obj.Foo.GetType() + " is not handled.", "obj");
否則,你應該要麼拋出一個InvalidOperationException
,或者創建自己的異常,如:
///<summary>The exception thrown because of ...</summary>
[Serializable]
public class MyException : Exception {
///<summary>Creates a MyException with the default message.</summary>
public MyException() : this("An error occurred") { }
///<summary>Creates a MyException with the given message.</summary>
public MyException (string message) : base(message) { }
///<summary>Creates a MyException with the given message and inner exception.</summary>
public MyException (string message, Exception inner) : base(message, inner) { }
///<summary>Deserializes a MyException .</summary>
protected MyException (SerializationInfo info, StreamingContext context) : base(info, context) { }
}
我建議你的名字UnsupportedTypeException。 :) – 2010-04-01 01:49:35