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(); } }
Author: coder
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; } } }
Gets the details of an exception suitable for display.
//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Text; namespace Redwerb.BizArk.Core.ExceptionExt { /// <summary> /// Extensions for classes within the Drawing namespace. /// </summary> public static class ExceptionExt { /// <summary> /// Gets the details of an exception suitable for display. /// </summary> /// <param name="ex"></param> /// <returns></returns> public static string GetDetails(this Exception ex) { var details = new StringBuilder(); while (ex != null) { details.AppendLine(ex.GetType().FullName); details.AppendLine(ex.Message); details.AppendLine(ex.StackTrace); ex = ex.InnerException; if (ex != null) { details.AppendLine(); details.AppendLine(new string('#', 70)); } } return details.ToString(); } } }
Returns the simple name of the class, for use in exception messages.
//****************************** // Written by Peter Golde // Copyright (c) 2004-2007, Wintellect // // Use and restribution of this code is subject to the license agreement // contained in the file "License.txt" accompanying this file. //****************************** using System; using System.Collections; using System.Collections.Generic; namespace Wintellect.PowerCollections { /// <summary> /// A holder class for various internal utility functions that need to be shared. /// </summary> internal static class Util { /// <summary> /// Returns the simple name of the class, for use in exception messages. /// </summary> /// <returns>The simple name of this class.</returns> public static string SimpleClassName(Type type) { string name = type.Name; // Just use the simple name. int index = name.IndexOfAny(new char[] { '<', '{', '`' }); if (index >= 0) name = name.Substring(0, index); return name; } } }
Format Error String
/////////////////////////////////////////////////////////////////////////////////////////////// // // This File is Part of the CallButler Open Source PBX (http://www.codeplex.com/callbutler // // Copyright (c) 2005-2008, Jim Heising // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation and/or // other materials provided with the distribution. // // * Neither the name of Jim Heising nor the names of its contributors may be // used to endorse or promote products derived from this software without specific prior // written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; namespace CallButler.Service.Utils { public class ErrorUtils { public static string FormatErrorString(Exception e) { StringBuilder sb = new StringBuilder(); Exception curExp = e; while (curExp != null) { sb.AppendFormat("Source: {0} Message: {1} Stack Trace: {2}", curExp.Source, curExp.Message, curExp.StackTrace); curExp = curExp.InnerException; if (curExp != null) sb.AppendLine("----------------------------------------"); } return sb.ToString(); } } }
Print the stack trace when an exception is thrown
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // StakTrce.cs -- demonstrates printing the stack trace when an // exception is thrown. // // Compile this program with the following command line: // C:>csc /debug:full StakTrce.cs using System; namespace nsExceptions { public class StakTrce { static public void Main () { clsTest test = new clsTest(); test.TestStackTrace (); } } public class clsTest { public void TestStackTrace () { try { CauseTrouble(1.7); } catch (Exception e) { Console.WriteLine (e.StackTrace); } } void CauseTrouble (double val) { clsAnother nudder = new clsAnother (); nudder.MakeProblem ((int) val); } } class clsAnother { public void MakeProblem (int x) { throw (new Exception()); } } }
Printing the stack trace from the Environment when an exception is not thrown
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // EnvTrace.cs -- demonstrates printing the stack trace from the Environment // when an exception is not thrown. // // Compile this program with the following command line: // C:>csc /debug:full EnvTrace.cs using System; using System.Diagnostics; namespace nsExceptions { public class EnvTrace { static public void Main () { clsTest test = new clsTest(); test.TestStackTrace (); Console.WriteLine (" Program completed normally"); } } public class clsTest { public void TestStackTrace () { try { CauseTrouble(1.7); } catch (Exception e) { Console.WriteLine (e.StackTrace); } } void CauseTrouble (double val) { clsAnother nudder = new clsAnother (); nudder.MakeProblem ((int) val); } } class clsAnother { public void MakeProblem (int x) { Console.WriteLine (Environment.StackTrace); } } }