using System; using System.Runtime.Serialization; class TestDirectoryMissingException : ApplicationException { public TestDirectoryMissingException() : base() { } public TestDirectoryMissingException(string msg) : base(msg) { } public TestDirectoryMissingException(SerializationInfo info, StreamingContext cxt) : base(info, cxt) { } public TestDirectoryMissingException(string msg, Exception inner) : base(msg, inner) { } } class MainClass { static void DoStuff() { if (System.IO.Directory.Exists("c: est") == false) throw new TestDirectoryMissingException("The test directory does not exist"); } static void Main(string[] args) { try { DoStuff(); } catch (System.IO.DirectoryNotFoundException e1) { System.Console.WriteLine(e1.StackTrace); System.Console.WriteLine(e1.Source); System.Console.WriteLine(e1.TargetSite); } catch (System.IO.FileNotFoundException e2) { } catch (System.Exception e) { System.Console.WriteLine("Ex"); } finally { System.Console.WriteLine("final"); } } }
Exception Handling User-Defined Exception Classes
/* A Programmer's Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3 */ // 04 - Exception HandlingUser-Defined Exception Classes // copyright 2000 Eric Gunnerson using System; public class UserDefinedExceptionClasses { public static void Main() { Summer summer = new Summer(); try { summer.DoAverage(); } catch (CountIsZeroException e) { Console.WriteLine("CountIsZeroException: {0}", e); } } } public class CountIsZeroException: ApplicationException { public CountIsZeroException() { } public CountIsZeroException(string message) : base(message) { } public CountIsZeroException(string message, Exception inner) : base(message, inner) { } } public class Summer { int sum = 0; int count = 0; float average; public void DoAverage() { if (count == 0) throw(new CountIsZeroException("Zero count in DoAverage")); else average = sum / count; } }
Exception Handling: The Exception Hierarchy 3
using System; public class ExceptionHierarchyOutOfRangeException { static int Zero = 0; static void AFunction() { try { int j = 22 / Zero; } // this exception doesn't match catch (ArgumentOutOfRangeException e) { Console.WriteLine("OutOfRangeException: {0}", e); } Console.WriteLine("In AFunction()"); } public static void Main() { try { AFunction(); } // this exception doesn't match catch (ArgumentException e) { Console.WriteLine("ArgumentException {0}", e); } } }
Exception Handling: The Exception Hierarchy 2
using System; public class ExceptionHierarchyNeverExecuted { static int Zero = 0; static void AFunction() { int j = 22 / Zero; Console.WriteLine("In AFunction()"); } public static void Main() { try { AFunction(); } catch (DivideByZeroException e) { Console.WriteLine("DivideByZero {0}", e); } } }
Exception Handling: The Exception Hierarchy 1
using System; public class ExceptionHierarchy { static int Zero = 0; public static void Main() { try { int j = 22 / Zero; } // catch a specific exception catch (DivideByZeroException e) { Console.WriteLine("DivideByZero {0}", e); } // catch any remaining exceptions catch (Exception e) { Console.WriteLine("Exception {0}", e); } } }
Using checked and unchecked with statement blocks
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Using checked and unchecked with statement blocks. using System; public class CheckedBlocks { public static void Main() { byte a, b; byte result; a = 127; b = 127; try { unchecked { a = 127; b = 127; result = unchecked((byte)(a * b)); Console.WriteLine("Unchecked result: " + result); a = 125; b = 5; result = unchecked((byte)(a * b)); Console.WriteLine("Unchecked result: " + result); } checked { a = 2; b = 7; result = checked((byte)(a * b)); // this is OK Console.WriteLine("Checked result: " + result); a = 127; b = 127; result = checked((byte)(a * b)); // this causes exception Console.WriteLine("Checked result: " + result); // won't execute } } catch (OverflowException exc) { // catch the exception Console.WriteLine(exc); } } }
refines the System.Exception base class with name of the type and time of exception to create your own Exception
using System; public class ConstructorException : Exception { public ConstructorException(object origin) : this(origin, null) { } public ConstructorException(object origin, Exception innerException) : base("Exception in constructor", innerException) { prop_Typename = origin.GetType().Name; prop_Time = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString(); } protected string prop_Typename = null; public string Typename { get { return prop_Typename; } } protected string prop_Time = null; public string Time { get { return prop_Time; } } } public class Starter { public static void Main() { try { MyClass obj = new MyClass(); } catch (ConstructorException except) { Console.WriteLine(except.Message); Console.WriteLine("Typename: " + except.Typename); Console.WriteLine("Occured: " + except.Time); } } } class MyClass { public MyClass() { // initialization fails throw new ConstructorException(this); } }