“This is the 29th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!

Write some thoughts of the programmer, hope it will help you.

Welcome to become my reader, hope this article can give you some help.

preface

The most important skill in everyday coding is not that you learn to use a lot of the latest programming techniques or build a great system. It’s how you handle exceptions while you’re writing code, whether the system is stable and robust.

Exception handling is captured in C# via the try/cath mechanism, so let’s take a look.

An exception is a runtime error in a program that violates system or application constraints, or that occurs in a way that was not anticipated during normal operation.

For example, abnormal assignment of some variables or errors occurred when participating in the operation.

abnormal

About exceptions, in C# through the try statement and handling the exception mechanism class to ensure that exceptions are captured, to prevent the entire program from being reminded of exceptions because of the exception is not captured, or even crash.

Abnormal requirements

  • The try block contains code that is protected against exceptions
  • The catch clause section contains one or more catch clauses. (Exception handling code snippets are also called exception handling subroutines)
  • The finally block contains code that should be executed inall cases, whether or not an exception occurs
Class Program {static void Main(string[] args) {try {Ahui hui = new Ahui(); InfoBase info = (InfoBase)hui; Console.WriteLine(hui.GetInfo()); Console.WriteLine(info.GetInfo()); } catch (Exception ex) {// Throw new Exception(" Exception thrown up one layer!" ); } Console.ReadKey(); }}Copy the code

When dealing with business code, logical errors such as nullates, abnormal values, value type errors, etc. must be considered for validation at the outset.

Secondly, try/catch mechanism is adopted to catch abnormal exceptions to prevent the program from uncontrollable errors, resulting in crash.

Tip: in the business program as little as possible to use exceptions to capture, if the conditions meet the first time to carry out early processing of exceptions. Exception handling in <Catch() takes longer and results in wasted system resources (exceptions are thrown up in real time). >

There are many different types of exceptions that can occur in C#, and BCL defines many classes, each representing a specified exception type. When an exception occurs, the CLR first creates an exception object of that type and then looks for the appropriate catch clause to handle it. (All Exception classes are derived from the System.exception class.)

There are three forms of catch() that handle different levels of exceptions.

The first kind of general exception handling

Try {} catch {//Copy the code

Any exception can be accepted, but the type causing the exception cannot be confirmed. Possible exceptions can be handled and cleaned up.

Second specific exception handling

Try} catch(Exception) {//Copy the code

Takes the name of an exception class as an argument that matches the exception of that specified class or an exception class derived from it.

The third kind of object-specific exception handling

    try
    
    }
    catch(Exception ex)
    {
        
      throw new Exception(ex.Message);
    }
Copy the code

Provides a variety of exception information about exceptions, matching the specified exception or an exception class derived from it. You can obtain detailed information about exceptions by using exception instances.

Try} catch (Exception ex) {// Throw new Exception(ex.Message); } finally {// exception handling mechanism}Copy the code

A special note about the code in finally is that it executes whether or not the exception is caught.

Finally is a good place to put code for releasing resources or subsequent processing.

Basic Knowledge day has been 29 days, if you still want to continue, you can give attention, thank you for your support.

remarks

Life is short. I don’t want to go for what I can’t see. I want to catch what I can see.

Original is not easy, give a attention.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.

I’m glad to make friends with you.