<%@ WebHandler Language="C#" Class="CRethrowHandler" %> using System; using System.Web; public class CRethrowHandler : IHttpHandler { private const string ID_QUERY_RETHROW_STYLE = "style"; private const string STYLE_THROW = "throw"; private const string STYLE_THROWEX = "throwex"; private const string STYLE_THROWINNER = "throwinner"; // -------------------------------------------------------------------------- public void ProcessRequest( HttpContext context ) { context.Response.ContentType = "text/plain"; if ( context.Request != null && context.Request.QueryString != null && context.Request.QueryString[ ID_QUERY_RETHROW_STYLE ] != null ) { try { switch ( context.Request.QueryString[ ID_QUERY_RETHROW_STYLE ] ) { case STYLE_THROW: ExecuteThrow(); break; case STYLE_THROWEX: ExecuteThrowEx(); break; case STYLE_THROWINNER: ExecuteThrowInner(); break; } } catch ( Exception ex ) { context.Response.Write( ex.StackTrace.Replace( Environment.NewLine, "
" ) ); if ( ex.InnerException != null ) { // There is an InnerException, display its stack too context.Response.Write( "
Inner stack trace
" + ex.InnerException.StackTrace.Replace( Environment.NewLine, "
" ) ); } } } else { context.Response.Write( "Empty request" ); } } // -------------------------------------------------------------------------- private void ExecuteThrow() { try { NestedMethod1(); } catch ( Exception ) { // Rethrow exception without loss Console.WriteLine( "Exception caught" ); throw; } } private void ExecuteThrowEx() { try { NestedMethod1(); } catch ( Exception ex ) { // Rethrow exception with loss Console.WriteLine( "Exception caught" ); throw ex; } } // -------------------------------------------------------------------------- private void NestedMethod1() { NestedMethod2(); } private void NestedMethod2() { NestedMethod3(); } private void NestedMethod3() { throw new ArgumentException( "This is a dummy exception only" ); } // -------------------------------------------------------------------------- private void ExecuteThrowInner() { try { NestedMethodInner1(); } catch ( Exception ex ) { Console.WriteLine( "Exception caught" ); throw ex; } } private void NestedMethodInner1() { try { NestedMethod2(); } catch ( Exception ex ) { // Wrap exception in another one. throw new ApplicationException( "Inner exception", ex ); } } // -------------------------------------------------------------------------- public bool IsReusable { get { return false; } } }