B
B
BadCats2016-09-27 11:34:59
C++ / C#
BadCats, 2016-09-27 11:34:59

SystemException specific?

Hello everyone, I started to study exceptions. I study video course.
There is such an example:

class Program
    {
        static void Main()
        {
            int a = 1, n = 2;

            try
            {
                // Попытка деления на ноль.
                a = a / (2 - n);

                Console.WriteLine("a = {0}", a);
            }
            catch (Exception e)
            {
                Console.WriteLine("Обработка исключения.");
                Console.WriteLine(e.Message);
            }

            // Delay.
            Console.ReadKey();
        }
    }

the author, through ObjectBrowser, demonstrates the hierarchy of exceptions in c#
1a1a82af064c44bd9570af5e933426a4.JPG
and says that when an exception of type DevideByZeroException occurs on a line, then when it enters the block, this type of exception will be reduced to the base type for all exceptions Exception (here I understood everything, because we ourselves as the block argument catch type Exception) then the author says that you can do it this way and that . I have a question: let's say in this case my exception of type DevideByZeroException will be cast in the catch block to type ArithmeticException , and during the compilation process all exceptions are still cast to type Exception , or No ?a = a / (2 - n);
catch (Exception e)
catch (DevideByZeroException e)
catch ( ArithmeticException e)
Then the author says that it is better not to use the SystemException exception type in the catch bokeh - because I quote "Microsoft themselves got confused from the first version of the language and now wish that they introduced the SystemException exception type at all" - is the author right? why (no need to write this is complete heresy - please justify your answer) - ? Maybe he meant something, the Respondents can rephrase him to express their conjectures about what the author meant.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Kuzmin, 2016-09-27
@BadCats

About the first question. No, all exceptions are not cast to a specific type at compile time. That is, a DevideByZeroException exception is thrown, and then, sequentially, in the catch blocks, the type to which this exception can be cast is searched.
For example, you can perform different actions on different exceptions. Code that catches an exception of type Exception will be executed only if the thrown exception cannot be cast to the types described earlier

try
{
  //Code
}
catch ( DivideByZeroException ex )
{
}
catch ( ArithmeticException ex )
{
}
catch ( Exception ex )
{
}

M
Maxim, 2016-10-16
@Got_Oxidus

Inheritance chain
The compiler may cast to a type that is higher. THOSE. DivideByZeroException can be cast to ArithmeticException and Exception, and ArithmeticException can only be cast to Exception, not DivideByZeroException.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question