/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example13_8.cs illustrates creating and
throwing an exception object
*/
using System;
public class Example13_8
{
public static void Main()
{
try
{
// create a new Exception object, passing a string
// for the Message property to the constructor
Exception myException = new Exception("myException");
// set the HelpLink and Source properties
myException.HelpLink = "See the Readme.txt file";
myException.Source = "My Example13_8 Program";
// throw the Exception object
throw myException;
}
catch (Exception e)
{
// display the exception object's properties
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);
}
}
}
Month: February 2011
Exception throw and catch 2
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace ExceptionHandling
{
public class TesterExceptionHandling3
{
static void Main()
{
Console.WriteLine("Enter Main...");
TesterExceptionHandling3 t = new TesterExceptionHandling3();
t.Run();
Console.WriteLine("Exit Main...");
}
public void Run()
{
Console.WriteLine("Enter Run...");
Func1();
Console.WriteLine("Exit Run...");
}
public void Func1()
{
Console.WriteLine("Enter Func1...");
try
{
Console.WriteLine("Entering try block...");
Func2();
Console.WriteLine("Exiting try block...");
}
catch
{
Console.WriteLine("Exception caught and handled!");
}
Console.WriteLine("Exit Func1...");
}
public void Func2()
{
Console.WriteLine("Enter Func2...");
throw new System.Exception();
Console.WriteLine("Exit Func2...");
}
}
}
Exception throw and catch
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace ExceptionHandling
{
public class TesterExceptionHandling2
{
static void Main()
{
Console.WriteLine("Enter Main...");
TesterExceptionHandling2 t = new TesterExceptionHandling2();
t.Run();
Console.WriteLine("Exit Main...");
}
public void Run()
{
Console.WriteLine("Enter Run...");
Func1();
Console.WriteLine("Exit Run...");
}
public void Func1()
{
Console.WriteLine("Enter Func1...");
Func2();
Console.WriteLine("Exit Func1...");
}
public void Func2()
{
Console.WriteLine("Enter Func2...");
try
{
Console.WriteLine("Entering try block...");
throw new System.Exception();
Console.WriteLine("Exiting try block...");
}
catch
{
Console.WriteLine("Exception caught and handled!");
}
Console.WriteLine("Exit Func2...");
}
}
}
Exception throws
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
namespace ExceptionHandling
{
public class TesterExceptionHandling1
{
static void Main()
{
Console.WriteLine("Enter Main...");
TesterExceptionHandling1 t = new TesterExceptionHandling1();
t.Run();
Console.WriteLine("Exit Main...");
}
public void Run()
{
Console.WriteLine("Enter Run...");
Func1();
Console.WriteLine("Exit Run...");
}
public void Func1()
{
Console.WriteLine("Enter Func1...");
Func2();
Console.WriteLine("Exit Func1...");
}
public void Func2()
{
Console.WriteLine("Enter Func2...");
throw new System.Exception();
Console.WriteLine("Exit Func2...");
}
}
}
Intentionally throws an error to demonstrate Just-In-Time debugging
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Throw.cs -- Intentionally throws an error to demonstrate
// Just-In-Time debugging.
//
// Compile this program with the following command line:
// C:>csc /debug:full Throw.cs
//
namespace nsThrow
{
using System;
public class Throw
{
static public void Main ()
{
Console.WriteLine ("This program intentionally causes an error.");
int x = 42;
int y = 0;
int z = x / y;
}
}
}
Throwing Your Own Exceptions
using System;
class MyException : ApplicationException
{
public MyException() : base("This is my exception message.")
{
}
}
class MainClass
{
public static void Main()
{
try
{
MainClass MyObject = new MainClass();
MyObject.ThrowException();
}
catch(MyException CaughtException)
{
Console.WriteLine(CaughtException.Message);
}
}
public void ThrowException()
{
throw new MyException();
}
}
Get Most Inner Exception
//The MIT License (MIT)
//http://arolibraries.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AroLibraries.ExtensionMethods
{
public static class ExceptionExt
{
public static Exception Ext_GetMostInner(this Exception ex)
{
Exception ActualInnerEx = ex;
while (ActualInnerEx != null)
{
ActualInnerEx = ActualInnerEx.InnerException;
if (ActualInnerEx != null)
ex = ActualInnerEx;
}
return ex;
}
}
}

