Define a class exception DivZero, if a division by zero, the abnormality information "is not the divisor 0" is displayed.

Title issues shown by "C # from entry to the master - Second Edition"

Chapter VII exception handling and debugging

Actual operating practice questions

Baidu relevant content PPT find some topics have mentioned, but there is no specific answer, because of the poor foundation, it is this simple thing takes up some time

 

 public class DivZero: Exception // define an exception class
    {
        public DivZero (STR String): Base (STR) has a base class inherit // constructor parameters
        {}
    }
    class Program
    {
      
        static void the Main (String [] args)
        {
            the try
            {             
                int X = the int.Parse (Console.ReadLine ()); // input divisor X
                IF (X == 0) // if X is 0, an exception is thrown, the program ends
                {
                    the throw new new DivZero ( "divisor is not 0 ");
                }
                int Y = 10;
                int Z = X / Y;
                Console.WriteLine (" {0} / {} = {2}. 1 ", X, Y, Z);
            }
            catch(Exception e)
            {               
                Console.WriteLine(e.Message);
            }           
            finally
            {
                Console.ReadKey();
            }
        }
    }

The main waste time, after the input X, not immediately throw an exception, but in the customary "  int Z = X / Y ;" re-thrown after

So the program directly executed by abnormal catch, did not meet the subject requirements.

Guess you like

Origin www.cnblogs.com/-young/p/12074953.html