Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Automatically re-raise Exceptions

If you wrap a call to HttpResponse.End within a try catch block, the ThreadAbortException would automatically be re-raised. I assume this is the case even if you wrap the try catch block in a try catch block.

How can I accomplish the same thing? I do not have a real-world application for this.

namespace Program
{
    class ReJoice
    {
        public void End() //This does not automatically re-raise the exception if caught.  
        {
            throw new Exception();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                ReJoice x = new ReJoice();
                x.End();
            }
            catch (Exception e) {}
        }
    }
}
like image 704
Brian Avatar asked Feb 04 '26 18:02

Brian


1 Answers

You can't change ordinary exceptions to have this behaviour. ThreadAbortException has special support for this that you can't implement yourself in C#.

ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

like image 51
Mark Byers Avatar answered Feb 06 '26 08:02

Mark Byers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!