locked
Inheritance RRS feed

  • Question

  • I have code:

    class Program
        {
            static void Main(string[] args)
            {
                new ExceptionsManager().Handle(null);
                Console.ReadLine();
            }
        }
    
        class MyAppException : ApplicationException { }
    
        class MyAppCriticalException : MyAppException { }
    
        class DatabaseUnavailableException : MyAppCriticalException { }
    
        class ExceptionsManager
        {
            public void Handle(MyAppCriticalException ex)
            {
                Console.WriteLine("Myappcriticalexception caught!");
            }
    
            public void Handle(DatabaseUnavailableException ex)
            {
                Console.WriteLine("Databaseunavailableexception caught!");
            }
    
            public void Handle(MyAppException ex)
            {
                Console.WriteLine("Myappexception caught!");
            }
        }

    I see message "Databaseunavailableexception caught!" on my screen, i understand that this is have place because of DatabaseUnavalaibleException is down in hierarchy, but i want to see full explanation, why i have this text on my screen.

    Help, please :)

    Sorry for my English.


    Thursday, July 4, 2013 1:23 PM

Answers

  • class Program
    {
        static void Main(string[] args)
        {
            new ExceptionsManager().Handle(new DatabaseUnavailableException());
            new ExceptionsManager().Handle(new MyAppCriticalException());
            new ExceptionsManager().Handle(new MyAppException());
            Console.ReadLine();
        }
    }
    
    class MyAppException : ApplicationException { }
    
    class MyAppCriticalException : MyAppException { }
    
    class DatabaseUnavailableException : MyAppCriticalException { }
    
    class ExceptionsManager
    {
        public void Handle(MyAppCriticalException ex)
        {
            Console.WriteLine("Myappcriticalexception caught!");
            var myAppEx = ex as MyAppException;
            if (myAppEx != null)
            {
                Handle(myAppEx);
            }
    
        }
    
        public void Handle(DatabaseUnavailableException ex)
        {
            Console.WriteLine("Databaseunavailableexception caught!");
            var myAppEx = ex as MyAppCriticalException;
            if (myAppEx != null)
            {
                Handle(myAppEx);
            }
        }
    
        public void Handle(MyAppException ex)
        {
            Console.WriteLine("Myappexception caught!");
        }
    }

    Try the above. I don't think this is a good pattern to use -but I think it achieves what you are looking to accomplish.

    Hope this helps,

    Dan

    Thursday, July 4, 2013 2:44 PM

All replies

  • Since the DataBase... class is the most specific inheritance, the code goes there first.

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Thursday, July 4, 2013 1:48 PM
  • public void Handle(DatabaseUnavailableException ex)
    {
                Console.WriteLine("Databaseunavailableexception caught : " + ex.ToString());
    }

    you can try to print the complete message.

    regards

    joon

    Thursday, July 4, 2013 1:53 PM
  • public void Handle(DatabaseUnavailableException ex)
    {
                Console.WriteLine("Databaseunavailableexception caught : " + ex.ToString());
    }

    you can try to print the complete message.

    regards

    joon

    This action will call NullReferenceException :)
    Thursday, July 4, 2013 2:14 PM
  • class Program
    {
        static void Main(string[] args)
        {
            new ExceptionsManager().Handle(new DatabaseUnavailableException());
            new ExceptionsManager().Handle(new MyAppCriticalException());
            new ExceptionsManager().Handle(new MyAppException());
            Console.ReadLine();
        }
    }
    
    class MyAppException : ApplicationException { }
    
    class MyAppCriticalException : MyAppException { }
    
    class DatabaseUnavailableException : MyAppCriticalException { }
    
    class ExceptionsManager
    {
        public void Handle(MyAppCriticalException ex)
        {
            Console.WriteLine("Myappcriticalexception caught!");
            var myAppEx = ex as MyAppException;
            if (myAppEx != null)
            {
                Handle(myAppEx);
            }
    
        }
    
        public void Handle(DatabaseUnavailableException ex)
        {
            Console.WriteLine("Databaseunavailableexception caught!");
            var myAppEx = ex as MyAppCriticalException;
            if (myAppEx != null)
            {
                Handle(myAppEx);
            }
        }
    
        public void Handle(MyAppException ex)
        {
            Console.WriteLine("Myappexception caught!");
        }
    }

    Try the above. I don't think this is a good pattern to use -but I think it achieves what you are looking to accomplish.

    Hope this helps,

    Dan

    Thursday, July 4, 2013 2:44 PM