Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I overload the throw keyword?

I want to overload the throw keyword to catch a class which inherits from Exception and to have it do some logging and other stuff before the actual throw. Is this possible? Or do I have to use a regular function?

I've tried:

public class cSilException : Exception
    {
        private string m_strMsg;

        public override void throw(cSilException ex)
        {

        }
...
...
}
like image 626
Amit Lipman Avatar asked Feb 08 '15 13:02

Amit Lipman


People also ask

What is throw keyword in Java?

The throws keyword in Java is used to declare exceptions that can occur during the execution of a program. For any method that can throw exceptions, it is mandatory to use the throws keyword to list the exceptions that can be thrown.

Does throw exit the function C++?

throw usually causes the function to terminate immediately, so you even if you do put any code after it (inside the same block), it won't execute. This goes for both C++ and C#.

How do you handle an exception thrown in Java?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

Does throwing an exception stop execution Java?

When an exception is thrown the method stops execution right after the "throw" statement. Any statements following the "throw" statement are not executed.


1 Answers

Register the event AppDomain.FirstChanceException. There you get all exceptions before the are actually thrown. In the event handler you can check for your exception and do the required logging.

No magic and no bad design.

class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_FirstChanceException;
        DoBadThings();
    }

    private static void DoBadThings()
    {
        DoOneLevelBelow();
    }

    private static void DoOneLevelBelow()
    {

        for(int i=0;i<10;i++)
        {
            try
            {
                if (i == 5)
                {
                    var invalidCast = (string)((object)i);
                }
                else
                {
                    throw new InvalidTimeZoneException();
                }
            }
            catch
            {

            }
        }
    }

    static void CurrentDomain_FirstChanceException(object sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
    {
        if( e.Exception is InvalidCastException)
        {
            LogInvalidCast((InvalidCastException)e.Exception);
        }
    }

    private static void LogInvalidCast(InvalidCastException invalidCastException)
    {
        Console.WriteLine("Got Invalid cast: {0}", invalidCastException);
    }

This will lead to:

Got invalid cast: System.InvalidCastException: The object of the type "System.Int32" cannot be converted to "System.String". at ThrowTest.Program.DoOneLevelBelow() in d:\Media\Blog\ThrowTest\Program.cs:line 31.

Please note since you are getting the exception before the stack is unwound you will see only the method where it did happen but not the calling methods since the stack was not unwound yet. If you want the complete call stack you can use Environment.StackTrace to get all stack frames.

like image 90
Alois Kraus Avatar answered Oct 14 '22 19:10

Alois Kraus