<?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%2FTry_Catch</id>
		<title>Csharp/CSharp Tutorial/Language Basics/Try Catch - История изменений</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%2FTry_Catch"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Try_Catch&amp;action=history"/>
		<updated>2026-04-29T20:33:34Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/CSharp_Tutorial/Language_Basics/Try_Catch&amp;diff=6550&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/Try_Catch&amp;diff=6550&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/Try_Catch&amp;diff=6551&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/Try_Catch&amp;diff=6551&amp;oldid=prev"/>
				<updated>2010-05-26T12:19:22Z</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 try, catch, and finally block without Exception class declaration==&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;
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;
      int zero = 0;&lt;br /&gt;
      Console.WriteLine(&amp;quot;In try block: attempting division by zero&amp;quot;);&lt;br /&gt;
      int myInt = 1 / zero;&lt;br /&gt;
      Console.WriteLine(&amp;quot;You never see this message!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    catch&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;In catch block: an exception was thrown&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    finally&lt;br /&gt;
    {&lt;br /&gt;
      Console.WriteLine(&amp;quot;In finally block: do any cleaning up here&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;In try block: attempting division by zero&lt;br /&gt;
In catch block: an exception was thrown&lt;br /&gt;
In finally block: do any cleaning up here&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Catch exception with wrong type inside a function==&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;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
    &lt;br /&gt;
    static void AFunction()&lt;br /&gt;
    {&lt;br /&gt;
        &lt;br /&gt;
        try {&lt;br /&gt;
            int Zero = 0;&lt;br /&gt;
            int j = 22 / Zero;&lt;br /&gt;
        } catch (ArgumentOutOfRangeException e) // this exception doesn&amp;quot;t match&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;OutOfRangeException: {0}&amp;quot;, e);&lt;br /&gt;
        }&lt;br /&gt;
        Console.WriteLine(&amp;quot;In AFunction()&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public static void Main()&lt;br /&gt;
    {&lt;br /&gt;
        try&lt;br /&gt;
        {&lt;br /&gt;
            AFunction();&lt;br /&gt;
        }&lt;br /&gt;
        // this exception doesn&amp;quot;t match&lt;br /&gt;
        catch (ArgumentException e)&lt;br /&gt;
        {&lt;br /&gt;
            Console.WriteLine(&amp;quot;ArgumentException {0}&amp;quot;, e);&lt;br /&gt;
        }catch(Exception ee){&lt;br /&gt;
            Console.WriteLine(&amp;quot;Exception {0}&amp;quot;, ee);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Exception System.DivideByZeroException: Attempted to divide by zero.&lt;br /&gt;
   at MainClass.AFunction()&lt;br /&gt;
   at MainClass.Main()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Catch statement without exception variable==&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;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
   static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      int x = 10, y = 0;&lt;br /&gt;
      &lt;br /&gt;
      try { &lt;br /&gt;
      &lt;br /&gt;
         x = x/y; &lt;br /&gt;
      &lt;br /&gt;
      }catch (System.IndexOutOfRangeException) { &lt;br /&gt;
      &lt;br /&gt;
         Console.WriteLine(&amp;quot;catch clause&amp;quot;); &lt;br /&gt;
      &lt;br /&gt;
      } finally { &lt;br /&gt;
      &lt;br /&gt;
         Console.WriteLine(&amp;quot;finally clause&amp;quot;); &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;Unhandled Exception: System.DivideByZeroException: Attempted to divide by zero.&lt;br /&gt;
   at MainClass.Main()&lt;br /&gt;
finally clause&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Catch System.NullReferenceException==&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;
class MainClass&lt;br /&gt;
{&lt;br /&gt;
   static void Main()&lt;br /&gt;
   {&lt;br /&gt;
      try { &lt;br /&gt;
      &lt;br /&gt;
         string str = null;&lt;br /&gt;
         str.ToUpper();&lt;br /&gt;
      &lt;br /&gt;
      }catch (System.NullReferenceException) { &lt;br /&gt;
      &lt;br /&gt;
         Console.WriteLine(&amp;quot;catch clause&amp;quot;); &lt;br /&gt;
      &lt;br /&gt;
      } finally { &lt;br /&gt;
      &lt;br /&gt;
         Console.WriteLine(&amp;quot;finally clause&amp;quot;); &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;catch clause&lt;br /&gt;
finally clause&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Exception Handling Fundamentals==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;C# exception handling is managed via four keywords: try, catch, throw, and finally.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;try {&lt;br /&gt;
        // block of code to monitor for errors&lt;br /&gt;
    }&lt;br /&gt;
    catch (ExcepType1 exOb) {&lt;br /&gt;
        // handler for ExcepType1 &lt;br /&gt;
    }&lt;br /&gt;
    catch (ExcepType2 exOb) {&lt;br /&gt;
        // handler for ExcepType2 &lt;br /&gt;
    }&lt;br /&gt;
    .&lt;br /&gt;
    .&lt;br /&gt;
    finally{&lt;br /&gt;
    &lt;br /&gt;
    }&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;If no exception is thrown by a try block, no catch statements will be executed and program control resumes after the catch statement.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Exception handling with trying and catching==&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;
class MainClass{&lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
    public static void Main(){&lt;br /&gt;
        Console.WriteLine(&amp;quot;Before catch&amp;quot;);&lt;br /&gt;
        int Zero = 0;&lt;br /&gt;
        try {&lt;br /&gt;
            int j = 22 / Zero;&lt;br /&gt;
        } catch (Exception e) {&lt;br /&gt;
            Console.WriteLine(&amp;quot;Exception &amp;quot; + e.Message);&lt;br /&gt;
        }&lt;br /&gt;
        &lt;br /&gt;
        Console.WriteLine(&amp;quot;After catch&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Before catch&lt;br /&gt;
Exception Attempted to divide by zero.&lt;br /&gt;
After catch&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Handle error gracefully and continue.==&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 MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
      try { &lt;br /&gt;
        int j= 0;&lt;br /&gt;
        int i = 5/j;&lt;br /&gt;
      } &lt;br /&gt;
      catch (DivideByZeroException) { &lt;br /&gt;
        // catch the exception &lt;br /&gt;
        Console.WriteLine(&amp;quot;Can&amp;quot;t divide by Zero!&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;Can&amp;quot;t divide by Zero!&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to handle a specific 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;
class MainClass{&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    try {&lt;br /&gt;
      int zero = 0;&lt;br /&gt;
      Console.WriteLine(&amp;quot;In try block: attempting division by zero&amp;quot;);&lt;br /&gt;
      int myInt = 1 / zero;&lt;br /&gt;
    } catch (DivideByZeroException myException) {&lt;br /&gt;
      Console.WriteLine(&amp;quot;Message = &amp;quot; + myException.Message);&lt;br /&gt;
      Console.WriteLine(&amp;quot;StackTrace = &amp;quot; + myException.StackTrace);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;In try block: attempting division by zero&lt;br /&gt;
Message = Attempted to divide by zero.&lt;br /&gt;
StackTrace =    at MainClass.Main()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Let the C# runtime system handle the error.==&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 MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    int[] nums = new int[4]; &lt;br /&gt;
 &lt;br /&gt;
    Console.WriteLine(&amp;quot;Before exception is generated.&amp;quot;); &lt;br /&gt;
 &lt;br /&gt;
    // Generate an index out-of-bounds exception. &lt;br /&gt;
    for(int i=0; i &amp;lt; 10; i++) { &lt;br /&gt;
      nums[i] = i; &lt;br /&gt;
      Console.WriteLine(&amp;quot;nums[{0}]: {1}&amp;quot;, i, nums[i]); &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;Before exception is generated.&lt;br /&gt;
nums[0]: 0&lt;br /&gt;
nums[1]: 1&lt;br /&gt;
nums[2]: 2&lt;br /&gt;
nums[3]: 3&lt;br /&gt;
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.&lt;br /&gt;
   at MainClass.Main()&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use a nested try block.==&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 MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 }; &lt;br /&gt;
    int d = 0;&lt;br /&gt;
 &lt;br /&gt;
    try { // outer try &lt;br /&gt;
      for(int i=0; i &amp;lt; 10; i++) { &lt;br /&gt;
        try { // nested try &lt;br /&gt;
          Console.WriteLine(numer[i] + &amp;quot; / &amp;quot; + &lt;br /&gt;
                             numer[i] + &amp;quot; is &amp;quot; + &lt;br /&gt;
                             numer[i]/d); &lt;br /&gt;
        } &lt;br /&gt;
        catch (DivideByZeroException) { &lt;br /&gt;
          // catch the exception &lt;br /&gt;
          Console.WriteLine(&amp;quot;Can&amp;quot;t divide by Zero!&amp;quot;); &lt;br /&gt;
        } &lt;br /&gt;
      } &lt;br /&gt;
    }  &lt;br /&gt;
    catch (IndexOutOfRangeException) { &lt;br /&gt;
      // catch the exception &lt;br /&gt;
      Console.WriteLine(&amp;quot;No matching element found.&amp;quot;); &lt;br /&gt;
      Console.WriteLine(&amp;quot;Fatal error -- program terminated.&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;Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
No matching element found.&lt;br /&gt;
Fatal error -- program terminated.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use multiple catch statements.==&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 MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    int[] numer = { 4, 8, 16}; &lt;br /&gt;
    int j=0;&lt;br /&gt;
    &lt;br /&gt;
    for(int i=0; i &amp;lt; 10; i++) { &lt;br /&gt;
      try { &lt;br /&gt;
        Console.WriteLine(numer[i] + &amp;quot; / &amp;quot; + &lt;br /&gt;
                           numer[i] + &amp;quot; is &amp;quot; + &lt;br /&gt;
                           numer[i]/j); &lt;br /&gt;
      } &lt;br /&gt;
      catch (DivideByZeroException) { &lt;br /&gt;
        // catch the exception &lt;br /&gt;
        Console.WriteLine(&amp;quot;Can&amp;quot;t divide by Zero!&amp;quot;); &lt;br /&gt;
      } &lt;br /&gt;
      catch (IndexOutOfRangeException) { &lt;br /&gt;
        // catch the exception &lt;br /&gt;
        Console.WriteLine(&amp;quot;No matching element found.&amp;quot;); &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;Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
Can&amp;quot;t divide by Zero!&lt;br /&gt;
No matching element found.&lt;br /&gt;
No matching element found.&lt;br /&gt;
No matching element found.&lt;br /&gt;
No matching element found.&lt;br /&gt;
No matching element found.&lt;br /&gt;
No matching element found.&lt;br /&gt;
No matching element found.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use the &amp;quot;catch all&amp;quot; catch statement.==&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 MainClass { &lt;br /&gt;
  public static void Main() { &lt;br /&gt;
    int[] numer = { 4, 8}; &lt;br /&gt;
    int d = 0;&lt;br /&gt;
    &lt;br /&gt;
    for(int i=0; i &amp;lt; 10; i++) { &lt;br /&gt;
      try { &lt;br /&gt;
        Console.WriteLine(numer[i] + &amp;quot; / &amp;quot; + &lt;br /&gt;
                           numer[i] + &amp;quot; is &amp;quot; + &lt;br /&gt;
                           numer[i]/d); &lt;br /&gt;
      } &lt;br /&gt;
      catch { &lt;br /&gt;
        Console.WriteLine(&amp;quot;Some exception occurred.&amp;quot;); &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;Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&lt;br /&gt;
Some exception occurred.&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>