Try: Catch.. Throw is the mechanism of handling anomalies in C #;
When we encode, whether in business logic processing or mathematical operations, there may be unexpected situations, such as in the basic arithmetic operation, there may be overflow or other than 0 problems, then this requires a mechanism to deal with this anomaly, enhance the performance and stability of the software, try catch throw is used to handle exceptions.
A try code block generally holds the business processing logic code, such as trying to open a database connection, when processing business operations, and so on, all the code inside the try code, once an unknown exception occurs, the code in the try block is terminated, but instead jumps into the catch, if not placed inside the try, Once an exception occurs, the program faces a crash.
Catch code block and try match appear to handle exceptions that occur in a try block.
You can have multiple catch after a try to perform the most matching one based on the exception type in the catch (exception type);
Can not follow catch after try, but must have followed finally (can also follow finally when there is catch on the try)
Finally: The code in the code block is definitely executed regardless of the code execution or exception, so you can put some code that closes the database connection or frees up memory here.
When do I use a try? Try using more may be effective to avoid the system crashes due to an exception, but excessive use of try will reduce system performance, to introduce you to the general process of try in the program run:
When the program runs to the start of a try, it holds an entry point for the address record in the stack, which is overhead when the try executes to find the entry point.
Give you a suggestion:
When designing to operations such as operations, logic processing, and business operations, you must use exception handling, which is not recommended when defining or instantiating variables.
C # Try...catch...throw