The life cycle of exception types and exception variables

Source: Internet
Author: User

1) Throw exceptions are types that can be made, numbers, strings, and class objects.

2) The exception for throw is of type, and catch is matched strictly by type.

3) Note The memory model of the exception object .

One, the traditional processing error

File binary copyint filecopy01 (char *filename2, char *filename1) {file *fp1= NULL,  *fp2 = NULL;FP1 = fopen (filename1, "RB" if (FP1 = = NULL) {return 1;} FP2 = fopen (filename2, "WB"), if (FP1 = = NULL) {return 2;} Char buf[256];int  Readlen, writelen;while ((Readlen = Fread (buf, 1, 0, FP1)) > 0)//If the data is read, it is greater than {Writelen = Fwr ITE (BUF, 1, Readlen, FP2); if (Readlen! = Readlen) {return 3;}} Fclose (FP1); fclose (FP2); return 0;} Test program void Main () {int ret;ret = FILECOPY01 ("C:/1.txt", "c:/2.txt"); if (ret!=0) {switch (ret) {case 1:printf ("Error opening source file!\n") ; break;case 2:printf ("error!\n when opening target file"), Break;case 3:printf ("Error copying file!\n"), break;default:printf ("An unknown error occurred!\n");

 

Second, throw int type exception

/File binary copyvoid filecopy02 (char *filename2, char *filename1) {file *fp1= NULL,  *fp2 = NULL;FP1 = fopen (filename1, "RB" ); if (FP1 = = NULL) {//return 1;throw 1;} FP2 = fopen (filename2, "WB"), if (FP1 = = NULL) {//return 2;throw 2;} Char buf[256];int  Readlen, writelen;while ((Readlen = Fread (buf, 1, 0, FP1)) > 0)//If the data is read, it is greater than {Writelen = Fwr ITE (BUF, 1, Readlen, FP2); if (Readlen! = Readlen) {//return 3;throw 3;}} Fclose (FP1); fclose (FP2); return;}

  

C. throw character type exception

File binary copyvoid filecopy03 (char *filename2, char *filename1) {file *fp1= NULL,  *fp2 = NULL;FP1 = fopen (filename1, "RB if (FP1 = = NULL) {Throw "Error opening source file";} FP2 = fopen (filename2, "WB"), if (FP1 = = NULL) {Throw "Error opening the destination file";} Char buf[256];int  Readlen, writelen;while ((Readlen = Fread (buf, 1, 0, FP1)) > 0)//If the data is read, it is greater than {Writelen = Fwr ITE (BUF, 1, Readlen, FP2); if (Readlen! = Readlen) {Throw "Copy file failed during";}} Fclose (FP1); fclose (FP2); return;}

  

Four, throw class object type exception

The throw int type variable//throw string type//throw class type class Badsrcfile {Public:badsrcfile () {cout << badsrcfile construct do << Endl;} ~badsrcfile () {cout << "Badsrcfile destructor Do" &LT;&LT;ENDL;} Badsrcfile (Badsrcfile & obj) {cout << "copy construction Do" &LT;&LT;ENDL;} void ToString () {cout << "aaaa" << Endl;}}; Class Baddestfile {};class badcpyfile {};; void Filecopy04 (char *filename2, char *filename1) {FILE *fp1= NULL, *fp2 = NULL;FP1 = fopen (filename1, "RB"); if (FP1 = = N ULL) {//throw new badsrcfile (); throw Badsrcfile ();} FP2 = fopen (filename2, "WB"), if (FP1 = = NULL) {throw baddestfile ();} Char Buf[256];int Readlen, writelen;while ((Readlen = Fread (buf, 1, 0, FP1)) > 0)//If the data is read, it is greater than {Writelen = fwrite (buf, 1, Readlen, FP2); if (Readlen! = Readlen) {throw badcpyfile ();}} Fclose (FP1); fclose (FP2); return;}

Main test Case//conclusion: The//c++ compiler generates an object by throw, and the C + + compiler executes the corresponding catch branch, which is equivalent to a function call and passes the argument to the parameter. void Main () {try{//filecopy02 ("C:/1.txt", "C:/2.txt"),//Filecopy03 ("C:/1.txt", "C:/2.txt"), filecopy04 ("C:/1.txt", " C:/2.txt ");} catch (int e) {printf ("exception occurred:%d \ n", e);} catch (const char * e) {printf ("Exception occurred:%s \ n", e);} catch (Badsrcfile *e) {e->tostring ();p rintf ("Exception: An error occurred while opening the source file!\n");} catch (Badsrcfile &e) {e.tostring ();p rintf ("Exception: An error occurred while opening the source file!\n");} catch (Baddestfile e) {printf ("Exception occurred: Error!\n opening the destination file");} catch (Badcpyfile e) {printf ("Exception occurred: Copy error!\n");} catch (...)//catch the slip through {printf ("An unknown exception has occurred! Catch the slip-through \ ");} Class Badsrcfile {};//class baddestfile {};//class badcpyfile {};;}

  

The hierarchy of exceptions (the application of inheritance in exceptions)

1. Exception is class – Create your own exception class

2. Exception derivation

3. Data in the exception: data members

4. Passing exceptions by reference

--use virtual functions in exceptions

V. Standard Program Library exceptions

Case 1: #include "iostream" using namespace std; #include <stdexcept>   class teacher{public:teacher (int age)  constructor that handles error {if (age >) {throw out_of_range ("too old") by exception mechanism;} This->age = age;} Protected:private:int age;}; void Mainxx () {Try{teacher T1 (102);} catch (Out_of_range e) {cout << e.what () << Endl;} Exception E;system ("Pause");}

  

Case 2class Dog{public:dog () {parr = new int[1024*1024*100];//4mb}private:int *parr;}; int Main31 () {Dog *pdog;try{for (int i=1; i<1024; i++)//40gb!{ Pdog = new Dog (); cout << i << ": New dog succeeded." << Endl;} catch (Bad_alloc err) {cout << "new Dog failed:" << err.what () << Endl;} return 0;}

  

Case 3

The life cycle of exception types and exception variables

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.