C # language -16 basic errors and exceptions

Sometimes the application because of the end-user environment action program initiated or run code error occurs, so the program must be able to handle any errors that might occur.

Exception classes
  in C #, when a particular error exception condition occurs, it will create or throw an exception object that contains information to help track the problem.
  All exception classes in the System namespace, except IOException class, CompositionException class and the classes derived from these two types go out.
    IOException class and its derived classes in the System.IO namespace. System.IO file read and write data processing
    CompositionException class and its derived class System.ComponentModel.Compostion namespace. Dynamic loading processing components and assemblies.

  Two important classes SystemException, ApplicationException. Both classes inherit from System.Exception, which is derived from System.Object System.Exception class
    SystemException class: typically used by the .NET runtime throws an exception, or thrown by almost all of the application exception.
    ApplicationException categories: from the base class defines the application exception classes. CLR thrown in some airports also inherited class to class. General recommendations directly custom exception class derived from the base class Exception

 Catch the exception

  For possible error in the information processing C # code, the procedure is generally divided into three code blocks try-catch-finally.
    try: the code normal execution. But this part of the code may encounter some errors.
    catch: code to handle error conditions of the execution of the normal code is wrong. It can be used to record error.
    finally: whether or not the error code will be executed. General contains the code to clean up resources. Statement block can not include a return statement. The statement block is optional.
  Execution flow :
    executing the try block, if not abnormal, the normal operation. If the execution of an exception, interrupt execution jumps to catch statement and begin execution. If the program is included in finally statements, regardless of whether the try, will be performed by the abnormal normal output.

  try-catch-finally
    may be omitted finally block
    may have any number of catch block.
    You can define filters

  System.Exception property

 

  Exception filter
    C # 6 of the new features. When capturing different exception types may be selected to perform the catch block, to achieve a different approach for providing different error.

        static  void the Main ( String [] args) 
        { 
            Stream Stream = null ;
             the try 
            { 
                // program code 
                Convert.ToInt32 ( Double .MaxValue); 
            } 
            the catch (IOException EX) When (ex.Message.Contains ( " ha " )) / / exception type and IO exception error is contained in the "ha" executes 
            {
                 String MSG = ex.Message; 
            } 
            the catch (IOException EX) // exception type is abnormal performing IO 
            {
                the throw  new new Exception ( " error message " , EX); // change abnormal rethrows 
            }
             the catch (Exception EX) When (the Filter (EX)) 
            { 
                the throw ; // rethrows 
            }
             the catch (Exception EX) / / abnormality is performed 
            {
                 the throw EX; // rethrows 
            }
             the finally 
            { 
                stream.Dispose (); 
            } 
            the Console.ReadKey (); 
        } 
        static  BOOL Filter(Exception ex)
        {
            return false;
        }

 

Guess you like

Origin www.cnblogs.com/liuxiansheng1024/p/12655665.html