Csharp/C Sharp by API/System/Exception — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Exception.HelpLink
using System;
class MainClass {
public static void Main() {
try {
int[] nums = new int[4];
Console.WriteLine("Before exception is generated.");
// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) {
nums[i] = i;
}
}
catch (IndexOutOfRangeException exc) {
Console.WriteLine("Standard message is: ");
Console.WriteLine(exc); // calls ToString()
Console.WriteLine("Stack trace: " + exc.StackTrace);
Console.WriteLine("Message: " + exc.Message);
Console.WriteLine("TargetSite: " + exc.TargetSite);
Console.WriteLine("Class defining member: {0}", exc.TargetSite.DeclaringType);
Console.WriteLine("Member type: {0}", exc.TargetSite.MemberType);
Console.WriteLine("Source: {0}", exc.Source);
Console.WriteLine("Help Link: {0}", exc.HelpLink);
}
Console.WriteLine("After catch statement.");
}
}
Exception.Source
using System;
class MainClass {
public static void Main() {
try {
int[] nums = new int[4];
Console.WriteLine("Before exception is generated.");
// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) {
nums[i] = i;
}
}
catch (IndexOutOfRangeException exc) {
Console.WriteLine("Standard message is: ");
Console.WriteLine(exc); // calls ToString()
Console.WriteLine("Stack trace: " + exc.StackTrace);
Console.WriteLine("Message: " + exc.Message);
Console.WriteLine("TargetSite: " + exc.TargetSite);
Console.WriteLine("Class defining member: {0}", exc.TargetSite.DeclaringType);
Console.WriteLine("Member type: {0}", exc.TargetSite.MemberType);
Console.WriteLine("Source: {0}", exc.Source);
Console.WriteLine("Help Link: {0}", exc.HelpLink);
}
Console.WriteLine("After catch statement.");
}
}
Exception.TargetSite
using System;
class MainClass {
public static void Main() {
try {
int[] nums = new int[4];
Console.WriteLine("Before exception is generated.");
// Generate an index out-of-bounds exception.
for(int i=0; i < 10; i++) {
nums[i] = i;
}
}
catch (IndexOutOfRangeException exc) {
Console.WriteLine("Standard message is: ");
Console.WriteLine(exc); // calls ToString()
Console.WriteLine("Stack trace: " + exc.StackTrace);
Console.WriteLine("Message: " + exc.Message);
Console.WriteLine("TargetSite: " + exc.TargetSite);
Console.WriteLine("Class defining member: {0}", exc.TargetSite.DeclaringType);
Console.WriteLine("Member type: {0}", exc.TargetSite.MemberType);
Console.WriteLine("Source: {0}", exc.Source);
Console.WriteLine("Help Link: {0}", exc.HelpLink);
}
Console.WriteLine("After catch statement.");
}
}
extends Exception
using System;
using System.Runtime.Serialization;
[Serializable]
public sealed class MyException : Exception
{
private string stringInfo;
private bool booleanInfo;
public MyException() : base() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception inner): base(message, inner) { }
private MyException(SerializationInfo info, StreamingContext context) : base(info, context)
{
stringInfo = info.GetString("StringInfo");
booleanInfo = info.GetBoolean("BooleanInfo");
}
public MyException(string message, string stringInfo, bool booleanInfo) : this(message)
{
this.stringInfo = stringInfo;
this.booleanInfo = booleanInfo;
}
public MyException(string message, Exception inner, string stringInfo, bool booleanInfo): this(message, inner)
{
this.stringInfo = stringInfo;
this.booleanInfo = booleanInfo;
}
public string StringInfo
{
get { return stringInfo; }
}
public bool BooleanInfo
{
get { return booleanInfo; }
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("StringInfo", stringInfo);
info.AddValue("BooleanInfo", booleanInfo);
base.GetObjectData(info, context);
}
public override string Message
{
get
{
string message = base.Message;
if (stringInfo != null)
{
message += Environment.NewLine + stringInfo + " = " + booleanInfo;
}
return message;
}
}
}
public class MainClass
{
public static void Main()
{
try
{
throw new MyException("Some error", "SomeCustomMessage", true);
}
catch (MyException ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.BooleanInfo);
Console.WriteLine(ex.StringInfo);
}
}
}