22. Getting started with exception handling in C #. try catch throw,
During software operation, if the normal operation of the software should not occur, the software becomes abnormal. At this time, we need to handle these exceptions. Or terminate the program to avoid more serious errors. Or you are prompted to make some changes so that the program can continue to run.
C # the programming language itself provides us with this exception handling mechanism.
Exceptions in C # are a special response to the program running, for example, try to divide by zero. Or try to convert a string "aaa" to an integer.
An exception provides a way to transfer control of a program from one part to another. C # exception handling is based on four keywords:Try,Catch,FinallyAndThrow.
- Try: A try block identifies a specific abnormal code block to be activated. Followed by one or more catch blocks.
- Catch: The program captures exceptions through the exception handling program. Catch keyword indicates exception capture.
- Finally: The finally block is used to execute a given statement, regardless of whether the exception is thrown or not. For example, if you open a file, the file will be closed no matter whether an exception occurs or not.
- Throw: When a problem occurs, the program throws an exception. Use the throw keyword.
Syntax
Assume that a block has an exception, and a method uses the try and catch keywords to catch the exception. The code in the try/catch Block is protected. Use the try/catch syntax as follows:
Try {// The statement that caused the exception} catch (ExceptionName e1) {// error handling code} catch (ExceptionName e2) {// error handling code} catch (ExceptionName eN) {// error handling code} finally {// statement to be executed}
C # provides many exceptions. You only need to know the most basic Exception here. In a catch statement, we only handle the most basic Exception.
Next, let's look at a specific example to better understand how C # handles exceptions.
You create a project. The solution name is Sloution22, the project name is Exercise22, and the project type is console program. Click Program. cs. Add the code to it.
Here I will briefly describe this code.
1. 27 rows-45 rows we have created a predictiontest method. In this method, you must enter an integer. If you enter a string, an exception occurs in our program.
2. In row 39, we throw an exception defined by ourselves with throw. Added some information we want to pass.
3. Line 41-line 44 finally statements are optional. Only when you need to release resources for example. The finally part is executed no matter whether exceptions occur.
4. Lines 13-23 call the predictiontest method. Capture exceptions at the same time.
5. If line 16 calls the ExceptionTest method, this statement will not be executed. Otherwise, the task is executed normally.
6. 21 lines output brief information about the exception
7. 22 rows output detailed exception information. For example, the error code location.
Running result
1. An exception occurs when the user inputs a string.
2. the user enters an integer and the program runs normally.
Exercise
1. Read the code and add comments to key points
2. Check the detailed error information to find out which line of code has a problem.
3. Remove row 39th throw new Exception ("conversion Exception"). Check the detailed error message again to see which line of code has a problem. Think about the reason. If you do not understand, please contact me.
4. Will the finally statement be executed? Remove the try catch statement of the main method and debug the program.
Pay-as-you-go answers to questions C # encountered during the course of getting started.
Zero-basic learning C # beginners, most problems can be solved through self-study or the Internet.
HoweverThere are still some problems that are not so easy to solve and become a stumbling block to your learning. In the end, it may even cause you to lose your learning experience. Generally, when you get started with C # programming, you will encounter more than 20 to 30 problems that you cannot easily solve.
What do you need most at this time?
Yes! A professional teacher will help you!
If you have any questions during this course, you can ask the author for help via qq or email.
Only 30 RMBYou can get three professional answers from the author of this tutorial. So you only need less than 300 yuan to successfully complete the C # entry, and then open the door to the software industry.
The payment method is very simple:
Scan the following QR code. Leave your QQ number or email address in the payment message.
Education is not cheap, but reliable! Truly smart people know how to spend money to make professional people serve themselves and improve their efficiency.
Instructor qq: 2040227296
Instructor email: 2040227296@qq.com