<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FLanguage_Basics%2FCustom_Exception</id>
		<title>Csharp/CSharp Tutorial/Language Basics/Custom Exception - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FCSharp_Tutorial%2FLanguage_Basics%2FCustom_Exception"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Custom_Exception&amp;action=history"/>
		<updated>2026-04-30T00:11:58Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Custom_Exception&amp;diff=6554&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Custom_Exception&amp;diff=6554&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:53Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Custom_Exception&amp;diff=6555&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Custom_Exception&amp;diff=6555&amp;oldid=prev"/>
				<updated>2010-05-26T12:19:55Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==A custom exception with HelpLink and Source==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
public class CustomException : ApplicationException&lt;br /&gt;
{&lt;br /&gt;
  public CustomException(string Message) : base(Message)&lt;br /&gt;
  {&lt;br /&gt;
    this.HelpLink = &amp;quot;See the Readme.txt file&amp;quot;;&lt;br /&gt;
    this.Source = &amp;quot;My Program&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
  public static void Main()&lt;br /&gt;
  {&lt;br /&gt;
    try&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Throwing a new CustomException object&amp;quot;);&lt;br /&gt;
      throw new CustomException(&amp;quot;My CustomException message&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    catch (CustomException e)&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;HelpLink = &amp;quot; + e.HelpLink);&lt;br /&gt;
      Console.WriteLine(&amp;quot;Message = &amp;quot; + e.Message);&lt;br /&gt;
      Console.WriteLine(&amp;quot;Source = &amp;quot; + e.Source);&lt;br /&gt;
      Console.WriteLine(&amp;quot;StackTrace = &amp;quot; + e.StackTrace);&lt;br /&gt;
      Console.WriteLine(&amp;quot;TargetSite = &amp;quot; + e.TargetSite);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Throwing a new CustomException object&lt;br /&gt;
HelpLink = See the Readme.txt file&lt;br /&gt;
Message = My CustomException message&lt;br /&gt;
Source = My Program&lt;br /&gt;
StackTrace =    at MainClass.Main()&lt;br /&gt;
TargetSite = Void Main()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Create your own exception class based on Exception==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Collections;&lt;br /&gt;
public class MyException : Exception&lt;br /&gt;
{&lt;br /&gt;
    public MyException( String reason, Exception inner ) :base( reason, inner ) {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        try {&lt;br /&gt;
            try {&lt;br /&gt;
                ArrayList list = new ArrayList();&lt;br /&gt;
                list.Add( 1 );&lt;br /&gt;
                Console.WriteLine( &amp;quot;Item 10 = {0}&amp;quot;, list[10] );&lt;br /&gt;
            }&lt;br /&gt;
            catch( ArgumentOutOfRangeException x ) {&lt;br /&gt;
                throw new MyException( &amp;quot;I&amp;quot;d rather throw this&amp;quot;,x ) ;&lt;br /&gt;
            }&lt;br /&gt;
            finally {&lt;br /&gt;
                Console.WriteLine( &amp;quot;Cleaning up...&amp;quot; );&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        catch( Exception x ) {&lt;br /&gt;
            Console.WriteLine( x );&lt;br /&gt;
            Console.WriteLine( &amp;quot;Done&amp;quot; );&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Cleaning up...&lt;br /&gt;
MyException: I&amp;quot;d rather throw this ---&amp;gt; System.ArgumentOutOfRangeException: Index was out of range.&lt;br /&gt;
Must be non-negative and less than the size of the collection.&lt;br /&gt;
Parameter name: index&lt;br /&gt;
   at System.Collections.ArrayList.get_Item(Int32 index)&lt;br /&gt;
   at MainClass.Main()&lt;br /&gt;
   --- End of inner exception stack trace ---&lt;br /&gt;
   at MainClass.Main()&lt;br /&gt;
Done&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==CustomException is an application exception that supports remoting.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Reflection;&lt;br /&gt;
using System.Runtime.Serialization;&lt;br /&gt;
[assembly: AssemblyVersion(&amp;quot;1.1.0.0&amp;quot;)]&lt;br /&gt;
[assembly: AssemblyCultureAttribute(&amp;quot;&amp;quot;)]&lt;br /&gt;
&lt;br /&gt;
[Serializable]&lt;br /&gt;
public class CustomException : Exception {&lt;br /&gt;
    public CustomException(): base(&amp;quot;custom exception&amp;quot;, null) {&lt;br /&gt;
        prop_Time = DateTime.Now.ToLongDateString() + &amp;quot; &amp;quot; + DateTime.Now.ToShortTimeString();&lt;br /&gt;
    }&lt;br /&gt;
    protected CustomException(SerializationInfo info,StreamingContext context)&lt;br /&gt;
        : base(info, context) {&lt;br /&gt;
        prop_Time = info.GetString(&amp;quot;Time&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public override void GetObjectData(SerializationInfo info, StreamingContext context) {&lt;br /&gt;
        info.AddValue(&amp;quot;Time&amp;quot;, prop_Time, typeof(string));&lt;br /&gt;
        base.GetObjectData(info, context);&lt;br /&gt;
    }&lt;br /&gt;
    protected string prop_Time = null;&lt;br /&gt;
    public string Time {&lt;br /&gt;
        get {&lt;br /&gt;
            return prop_Time;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Derived exceptions must appear before base class exceptions.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System; &lt;br /&gt;
 &lt;br /&gt;
class MyException : ApplicationException { &lt;br /&gt;
  public MyException() : base() { } &lt;br /&gt;
  public MyException(string str) : base(str) { } &lt;br /&gt;
 &lt;br /&gt;
  public override string ToString() { &lt;br /&gt;
    return Message; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
class MyDerivedException : MyException { &lt;br /&gt;
  public MyDerivedException() : base() { } &lt;br /&gt;
  public MyDerivedException(string str) : base(str) { } &lt;br /&gt;
 &lt;br /&gt;
  public override string ToString() { &lt;br /&gt;
    return Message;  &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
class MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    for(int x = 0; x &amp;lt; 3; x++) { &lt;br /&gt;
      try { &lt;br /&gt;
        if(x==0) &lt;br /&gt;
           throw new MyException(&amp;quot;Caught an MyException exception&amp;quot;); &lt;br /&gt;
        else if(x==1) &lt;br /&gt;
           throw new MyDerivedException(&amp;quot;Caught an MyDerivedException exception&amp;quot;); &lt;br /&gt;
        else &lt;br /&gt;
           throw new Exception(); &lt;br /&gt;
      } &lt;br /&gt;
      catch (MyDerivedException exc) { &lt;br /&gt;
        // catch the exception &lt;br /&gt;
        Console.WriteLine(exc); &lt;br /&gt;
      } &lt;br /&gt;
      catch (MyException exc) { &lt;br /&gt;
        // catch the exception &lt;br /&gt;
        Console.WriteLine(exc); &lt;br /&gt;
      } &lt;br /&gt;
      catch (Exception exc) { &lt;br /&gt;
        Console.WriteLine(exc); &lt;br /&gt;
      } &lt;br /&gt;
    } &lt;br /&gt;
  } &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Caught an MyException exception&lt;br /&gt;
Caught an MyDerivedException exception&lt;br /&gt;
System.Exception: Exception of type &amp;quot;System.Exception&amp;quot; was thrown.&lt;br /&gt;
   at MainClass.Main()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Extends Exception==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
using System.Runtime.Serialization;&lt;br /&gt;
[Serializable]&lt;br /&gt;
public sealed class MyException : Exception&lt;br /&gt;
{&lt;br /&gt;
    private string stringInfo;&lt;br /&gt;
    private bool booleanInfo;&lt;br /&gt;
    public MyException() : base() { }&lt;br /&gt;
    public MyException(string message) : base(message) { }&lt;br /&gt;
    public MyException(string message, Exception inner): base(message, inner) { }&lt;br /&gt;
    private MyException(SerializationInfo info, StreamingContext context) : base(info, context)&lt;br /&gt;
    {&lt;br /&gt;
        stringInfo = info.GetString(&amp;quot;StringInfo&amp;quot;);&lt;br /&gt;
        booleanInfo = info.GetBoolean(&amp;quot;BooleanInfo&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public MyException(string message, string stringInfo, bool booleanInfo) : this(message)&lt;br /&gt;
    {&lt;br /&gt;
        this.stringInfo = stringInfo;&lt;br /&gt;
        this.booleanInfo = booleanInfo;&lt;br /&gt;
    }&lt;br /&gt;
    public MyException(string message, Exception inner, string stringInfo, bool booleanInfo): this(message, inner)&lt;br /&gt;
    {&lt;br /&gt;
        this.stringInfo = stringInfo;&lt;br /&gt;
        this.booleanInfo = booleanInfo;&lt;br /&gt;
    }&lt;br /&gt;
    public string StringInfo&lt;br /&gt;
    {&lt;br /&gt;
        get { return stringInfo; }&lt;br /&gt;
    }&lt;br /&gt;
    public bool BooleanInfo&lt;br /&gt;
    {&lt;br /&gt;
        get { return booleanInfo; }&lt;br /&gt;
    }&lt;br /&gt;
    public override void GetObjectData(SerializationInfo info, StreamingContext context)&lt;br /&gt;
    {&lt;br /&gt;
        info.AddValue(&amp;quot;StringInfo&amp;quot;, stringInfo);&lt;br /&gt;
        info.AddValue(&amp;quot;BooleanInfo&amp;quot;, booleanInfo);&lt;br /&gt;
        base.GetObjectData(info, context);&lt;br /&gt;
    }&lt;br /&gt;
    public override string Message&lt;br /&gt;
    {&lt;br /&gt;
        get&lt;br /&gt;
        {&lt;br /&gt;
            string message = base.Message;&lt;br /&gt;
            if (stringInfo != null)&lt;br /&gt;
            {&lt;br /&gt;
                message += Environment.NewLine + stringInfo + &amp;quot; = &amp;quot; + booleanInfo;&lt;br /&gt;
            }&lt;br /&gt;
            return message;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class MainClass&lt;br /&gt;
{&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        try&lt;br /&gt;
        {&lt;br /&gt;
            throw new MyException(&amp;quot;Some error&amp;quot;, &amp;quot;SomeCustomMessage&amp;quot;, true);&lt;br /&gt;
        } &lt;br /&gt;
        catch (MyException ex)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(ex.Message);&lt;br /&gt;
            Console.WriteLine(ex.BooleanInfo);&lt;br /&gt;
            Console.WriteLine(ex.StringInfo);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Some error&lt;br /&gt;
SomeCustomMessage = True&lt;br /&gt;
True&lt;br /&gt;
SomeCustomMessage&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a custom Exception==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;  &lt;br /&gt;
  &lt;br /&gt;
class RangeArrayException : ApplicationException { &lt;br /&gt;
  public RangeArrayException() : base() { } &lt;br /&gt;
  public RangeArrayException(string str) : base(str) { }  &lt;br /&gt;
 &lt;br /&gt;
  public override string ToString() { &lt;br /&gt;
    return Message; &lt;br /&gt;
  } &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
      &lt;br /&gt;
class MainClass {   &lt;br /&gt;
  public static void Main() {   &lt;br /&gt;
    try { &lt;br /&gt;
         throw new RangeArrayException(&amp;quot;Low index not less than high.&amp;quot;);  &lt;br /&gt;
    } catch (RangeArrayException exc) { &lt;br /&gt;
       Console.WriteLine(exc); &lt;br /&gt;
    } &lt;br /&gt;
  }  &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Low index not less than high.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==User-Defined Exception Classes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;using System;&lt;br /&gt;
public class CountIsZeroException: ApplicationException{&lt;br /&gt;
    public CountIsZeroException(){&lt;br /&gt;
    }&lt;br /&gt;
    public CountIsZeroException(string message) : base(message)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
    public CountIsZeroException(string message, Exception inner) : base(message, inner)&lt;br /&gt;
    {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MainClass{&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        try {&lt;br /&gt;
            DoAverage();&lt;br /&gt;
        }&lt;br /&gt;
        catch (CountIsZeroException e)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;CountIsZeroException: {0}&amp;quot;, e);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public static void DoAverage() {&lt;br /&gt;
        throw(new CountIsZeroException(&amp;quot;Zero count in DoAverage&amp;quot;));&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;CountIsZeroException: CountIsZeroException: Zero count in DoAverage&lt;br /&gt;
   at MainClass.DoAverage()&lt;br /&gt;
   at MainClass.Main()&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>