C #. NET Programming Foundation-try-catch exception Handling-factorial example

Source: Internet
Author: User
In the software development process, the program exception is very common, that is, we often say the bug, so we need to deal with unpredictable exceptions.
Exception handling is actually very simple, at first I also have to be difficult, do not fear, really very simple, really understand the most also on a try-catch statement and throw statement.
Try-catch Statement Usage:
try{
Statement block 1//code that could throw an exception

catch (Exception type 1 exception object 1) {
Statement Block 2//exception handling

catch (Exception type 2 exception object 2) {
Statement block 3//exception handling

When an exception occurs in a try statement block, the program first creates an exception object and then searches for the matching catch object, and if a matching block of code is found, the program jumps to the catch block and executes the statement in the block.
Here's an example of exception handling for factorial:
1, first give everyone did not do exception handling code:


I will not cut, when we enter in the input box is not a number, the system will produce an exception, the program is interrupted. This is the most common input exception.
2, the following is a Try-catch statement to the possible exception of the statement written in a try statement block, in the catch statement block processing:


This is when we do not enter a number in the input box will pop up a prompt box said that I format input is not correct, this is the exception processing.

C #. NET Programming Foundation-try-catch exception Handling-factorial example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.