C + + exception handling

Source: Internet
Author: User
Tags throw exception

In the process of programming, we always want to design the program is seamless, but this is almost impossible. Even if the program is compiled and implemented with the required functionality, it does not mean that the program is perfect, because it is possible to run the program with exceptions, such as when we design a program that calculates division for the user, the user is likely to enter the divisor as zero. Another example is when we need to open a file to find that the file has been deleted ... There are many similar situations where it is not possible to prevent them from being protected against these special circumstances.

We usually hope that the program we write can be handled in an unusual way, without the program interrupting or aborting the operation somehow. When designing a program, you should take full account of the various anomalies and deal with them.

In C + +, a function can detect an exception and return an exception, a mechanism known as throwing an exception. When an exception is thrown, the function caller catches the exception and processes the exception, which we call an exception capture.

The new C + + throw keyword is used to throw exceptions, the new catch keyword is used to catch exceptions, and the Try keyword tries to catch exceptions. You typically place the statement you are trying to capture in a try{} block, and you place the exception-handling statement in a catch{} statement block.

The basic syntax for exception handling is described below. Let's start by saying the basic syntax for throwing Exceptions:
The throw exception is composed of the throw keyword plus an expression. Exceptions and exception handlers need to be caught after an exception is thrown, with the following basic syntax:
Statements that may throw exceptions
catch (Exception type 1)
Handler for Exception type 1
catch (Exception type 2)
Handler for Exception type 2
catch (Exception type N)
handler for exception type N

The exception thrown by the throw is captured by the try program block and then the exception handler in the catch block is run according to the exception type. The Catch program block order can be arbitrary, but it needs to be placed after the try program block.

#include <iostream>using namespacestd;enumindex{underflow, overflow};intArray_index (int*a,intNintindex);intMain () {int*a =New int[Ten];  for(intI=0; i<Ten; i++) A[i]=i; Try{cout<<array_index (A,Ten,5) <<Endl; cout<<array_index (A,Ten, A) <<Endl; cout<<"DDD"<<Endl; cout<<array_index (A,Ten,-5) <<Endl; }    Catch(index e) {if(E = =underflow) {cout<<"Index underflow!"<<Endl; Exit (-1); }        if(E = =overflow) {cout<<"Index overflow!"<<Endl; Exit (-1); }    }    return 0;}intArray_index (int*a,intNintindex) {    if(Index <0)Throwunderflow; if(Index > N-1)Throwoverflow; returnA[index];}

This example shows an array of exception-trapping programs that are out of bounds. The Array_index function returns the value of the index subscript of the array and throws an exception if an exception occurs. A program statement in a try block is a statement that may have an exception condition, and a catch is a processing statement for the exception. At the beginning of the program we defined a global enumeration type variable index, and defined two values, underflow and overflow, respectively, as the return value of the throw exception. When the main function requires an array value that is out of bounds, the Array_index function is called, and once a predetermined exception is thrown, it is captured by a try and handled according to the exception condition based on the catch statement.

C + + exception handling

Related Article

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.