Exception handling and file IO in C + +

Source: Internet
Author: User
Tags throw exception

First, exception handling
1, what is the exception: can meet but unavoidable error
2. How to throw an exception
throw data;
A, you can throw a primitive type of exception
Throw-1;
Throw "I am abnormal";

B, can throw exception of class type
Throw Student Stu;

C, do not throw the exception of the pointer of the local object
Student Stu;
Throw &stu;

3. How to Catch exceptions
Try
{
Code that could produce an exception
}
catch (Exception type 1)
{
Exception handling Code 1
}
catch (Exception type 2)
{
Exception handling Code 2
}
catch (Exception Type 3)
{
Exception handling Code 3
}

A, not only the exception can be obtained when catching the exception, but also can get the exception data thrown
b, if the exception is thrown, but the program will end without being captured
C, the catch of the exception is top-down, not the best choice, so the subclass's exception capture is best placed in front of the parent class
D, to catch the exception as far as possible to use the way the reference, because when the exception is thrown when the object is used to capture the object's copy construction, so that the copy of the object in the process of reference to throw an exception

4. Exception of class type
A, you can define for each exception a class that does nothing, it is just to distinguish between various exceptions
b, the exception may be thrown when the construction, copy construction, assignment constructs, etc., if there is no visible resources in the class, we must implement these three functions
C, in order to prevent a custom exception can not be caught, so when we define the exception, it is best to inherit the exception class in the standard library, so that even if, can not accurately catch the exception, can not miss the exception
D, when throwing an exception, try to throw an anonymous temporary object

5, the compiler will generate a section to apply for "safe zone" code and protect it, in the event of an exception, the rhythm of the program has been interrupted, no location is safe except for the security zone, the security zone can ensure that the exception object stored in this location is not compromised

6. Exceptions in constructs and destructors
After an exception occurs in the constructor, it jumps directly to the exception handling code, the construction of the exception is interrupted, the object's construction is incomplete, and the incomplete object is never able to call the destructor, even if the call is displayed with delete[]

Exceptions in constructors can be thrown, but do not throw constructors (internal processing), generally using a rollback mechanism

Ii.. File io
Classes for manipulating files: Ifstream, Ofstream, FStream
Open File
How to open a file
In Read Ifstream/fstream
Out Write Ofstream/fstream
The app opens in Append mode, the file does not exist and is not created, there is no empty
Ate location at end of file when opening file
Binary opens file in binary mode
Trunc open a file with empty mode
In|out
Read and write using <<,>>, similar to cout, CIN
Read binary mode reading file
Write binary writes a file
Seek to adjust the position of the file pointer

Exception handling and file IO in C + +

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.