.


:




:

































 

 

 

 


, dynamic_cast. . Visitor




, dynamic_cast. . Visitor.

, , 2 7 dynamic_cast, . . nullptr:

 

class FilesystemEntry

{

//...

public:

//...

virtual File * asFile () { return nullptr; }

virtual Directory * asDirectory () { return nullptr; }

};

 

, . , - :

 

class File: public FilesystemEntry

{

//...

public:

//...

File * asFile () override { return this; }

};

 

class Directory: public FilesystemEntry

{

//...

public:

//...

Directory * asDirectory () override { return this; }

};

 

dynamic_cast:

 

FilesystemEntry * pEntry = new File(1.txt, 50);

File * pFile = pEntry-> asFile ();

assert(pFile);

 

(, SymbolicLink - ), FilesystemEntry .

 

Visitor (), . , visit, . - - .

, -. , :

:

, ;

(FilesystemEntry, File, Directory) ;

, - ;

, , SymbolicLink, , accept/visit :

o accept SymbolicLink;

o visit ;

o -;

 

, .

 

. , . . , . .

 

.

 

:

 

- ;

- , , ;

- ;

- .

 

. try-catch:

 

try { -- -, , . ,.. }

catch ( ) { }

 

catch , catch, - terminate, - abort, .

. , - - .

 

= catch. :

 

try {
//
}
catch (int param) { cout << "int exception"; }
catch (char param) { cout << "char exception"; }
catch (...) { cout << "default exception"; }

 

, , () catch, -. , . :

● ;

● ;

● , ;

● , ;

● const ,

 

:

catch (...) =

{

std::cerr << Fatal error. Please contact [email protected] << std::endl; }

 

O .

catch (...) catch( ) , , .. .

 

throw

try { throw 20;}, .

 

, .

- throw , . - . - .

 

 

. main runProgram, , . runProgram , main :

 

void runProgram ()

{

Try

{

ManagedIntegerArray a(10);

a[ rand() ] = 25;

}

catch (ManagedIntegerArray::IndexOutOfRange & e)

{

std::cout << Program has a problem with array index << std::endl;

}
}

 

int main ()

{

Try

{

runProgram();

}

catch (...)

{

std::cout << Program has some unknown problem << std::endl;

}
}

 

. catch , , .., terminate.

 

. catch , , . (rethrow), throw . :

 

void runProgram ()

{

Try

{

ManagedIntegerArray a(10);

a[ rand() ] = 25;

}

catch (ManagedIntegerArray::IndexOutOfRange & e)

{

//

std::cout << Program has a problem with array index << std::endl;

 

//

throw;

}
}

 

, ( throw ), termin

 

:

 

Try

{

throw new DerivedException(0, 0);

}

catch (DerivedException * _pE)

{

delete _pE;

}

 

-, , , . -, , , globalException. , .

 

- . , ( , ). , , - . , - , , , . , , , . , -.

 

.. , , , std::exception.

std::exception <exception>. std::exception , . , :

● ;

● ;

● ;

● what(), C, .

what() .

 

:

std::bad_alloc new, ;

 

std::bad_cast dynamic_cast, , , ;

 

std::bad_typeid typeid, ;

 

std::out_of_range std::vector std::deque ;

 

std::ios_base::failure / - , , exceptions, :

 

std::ifstream file;
file.exceptions (std::ifstream::failbit | std::ifstream::badbit);

 

std::logic_error std::runtime_error std::string, . , what() , .

 

std::logic_error , . , , . std::runtime_error, , , , . , -. , - - std::exception.

 

stack unwinding . . . . .

 

stack unwinding (, ).

, 2 :

1. .

2. .

, , .

, .

:

 

void f ()

{

Date d1;

if (d1.GetDay() == 1)

throw std::runtime_error(Cannot call f() on the 1st day of the month);

 

Date d2(2013, 12, 23);

//...

}

 

, . d1 , d2 . stack unwinding , d1, d2.

 

. , , , f, d2, d1:

 

void f ()

{

Date d1;

if (d1.GetDay() == 1)

throw std::runtime_error(Cannot call f() on the 1st day of the month);

 

Date d2(2013, 12, 23);

//...

 

// Date::~Date(& d2);

// Date::~Date(& d1);

}

 

. , d1 , d2 , - , , , .

 

. 3 :

● 0 - ;

● 1 - d1, d2;

● 2 - d1 d2.

, (, 1 ), . , . .

 

, :

 

void f ()

{

Date * pDate = new Date();

 

Try

{

if (pDate->GetDay() == 1)

throw std::runtime_error(...);

 

//...

 

//

delete pDate;

}

catch (...)

{

//

delete pDate;

 

//

throw;

}

}

 

void g ()

{

FILE * file = fopen(test.txt, rt);
try

{

if (some_condition)

throw std::runtime_error(Condition failed);

 

//

 

//

fclose ( file );

}

catch (...)

{

//

fclose ( file );

 

//

throw;

}

}

 

 

, .

 

, , ., Date , :

Try

{

Date d1(2013, 14, 1); // 14

 

// :

// Date::~Date(& d1);

}

catch (...)

{

}

 

, , delete:

 

Try

{

Date * pDate = new Date(2013, 14, 1); // 14

 

// , delete

}

catch (...)

{

}

 

, , . , .

 

. . , , , , stack unwinding, . , .

 

, std::uncaught_exception, , stack unwinding .

 

. catch ( catch ) , , . (rethrow), throw . :

 

void runProgram ()

{

Try

{

ManagedIntegerArray a(10);

a[ rand() ] = 25;

}

catch (ManagedIntegerArray::IndexOutOfRange & e)

{

//

std::cout << Program has a problem with array index << std::endl;

 

//

throw;

}
}

 

, , terminate ( abort, ).

 





:


: 2016-07-29; !; : 713 |


:

:

, .
==> ...

1680 - | 1497 -


© 2015-2024 lektsii.org - -

: 0.113 .