Csharp/CSharp Tutorial/Language Basics/Exception Throw — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:19, 26 мая 2010
Содержание
Creating and throwing an exception object
<source lang="csharp">using System; class MainClass {
public static void Main() { try { Exception myException = new Exception("myException"); myException.HelpLink = "See the Readme.txt file"; myException.Source = "My Program"; throw myException; } catch (Exception e) { Console.WriteLine("HelpLink = " + e.HelpLink); Console.WriteLine("Message = " + e.Message); Console.WriteLine("Source = " + e.Source); Console.WriteLine("StackTrace = " + e.StackTrace); Console.WriteLine("TargetSite = " + e.TargetSite); } }
}</source>
HelpLink = See the Readme.txt file Message = myException Source = My Program StackTrace = at MainClass.Main() TargetSite = Void Main()
Manually throw an exception.
The general form is:
<source lang="csharp">throw exceptOb;</source>
Rethrow an exception
<source lang="csharp">using System;
class MainClass {
public static void Main() { try { genException(); } catch(IndexOutOfRangeException) { // recatch exception Console.WriteLine("Fatal error -- " + "program terminated."); } } public static void genException() { int[] numer = { 4, 8}; int d = 0; for(int i=0; i<10; i++) { try { Console.WriteLine(numer[i] + " / " + numer[i] + " is " + numer[i]/d); } catch (DivideByZeroException) { // catch the exception Console.WriteLine("Can"t divide by Zero!"); } catch (IndexOutOfRangeException) { // catch the exception Console.WriteLine("No matching element found."); throw; // rethrow the exception } } }
}</source>
Can"t divide by Zero! Can"t divide by Zero! No matching element found. Fatal error -- program terminated.
Throw Exception from a function
<source lang="csharp">using System; class MainClass {
static void AFunction() { int Zero = 0; int j = 22 / Zero; } public static void Main() { try { Console.WriteLine("efore Function"); AFunction(); } catch (DivideByZeroException e) { Console.WriteLine("DivideByZero {0}", e); } }
}</source>
efore Function DivideByZero System.DivideByZeroException: Attempted to divide by zero. at MainClass.Main()
Throw Exception in finally statement
<source lang="csharp">using System; using System.Collections; public class MainClass {
static void Main() { try { try { ArrayList list = new ArrayList(); list.Add( 1 ); Console.WriteLine( "Item 10 = {0}", list[10] ); } finally { Console.WriteLine( "Cleaning up..." ); throw new Exception( "I like to throw" ); } } catch( ArgumentOutOfRangeException ) { Console.WriteLine( "Oops! Argument out of range!" ); } catch { Console.WriteLine( "Done" ); } }
}</source>
Cleaning up... Done
Wrap exception in another one, adding additional context
<source lang="csharp">using System; public class MyClass {
public void DoAverage() { try { int count = 0; int average = 10 / count; } catch (DivideByZeroException e) { throw (new DivideByZeroException( "Count is zero in DoAverage()", e)); } }
} public class MainClass {
public static void Main() { MyClass my = new MyClass(); try { my.DoAverage(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e); } }
}</source>
Exception: System.DivideByZeroException: Count is zero in DoAverage() ---> System.DivideByZeroExcept ion: Attempted to divide by zero. at MyClass.DoAverage() --- End of inner exception stack trace --- at MyClass.DoAverage() at MainClass.Main()