mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-26 00:31:39 +03:00
Merge pull request #1123 from adamheins/master
[c++/en] Make code compileable.
This commit is contained in:
commit
9e44a5b9ee
@ -445,6 +445,7 @@ int main () {
|
|||||||
// define a class or function that takes a type parameter:
|
// define a class or function that takes a type parameter:
|
||||||
template<class T>
|
template<class T>
|
||||||
class Box {
|
class Box {
|
||||||
|
public:
|
||||||
// In this class, T can be used as any other type.
|
// In this class, T can be used as any other type.
|
||||||
void insert(const T&) { ... }
|
void insert(const T&) { ... }
|
||||||
};
|
};
|
||||||
@ -518,12 +519,13 @@ printMessage<10>(); // Prints "Learn C++ faster in only 10 minutes!"
|
|||||||
// (see http://en.cppreference.com/w/cpp/error/exception)
|
// (see http://en.cppreference.com/w/cpp/error/exception)
|
||||||
// but any type can be thrown an as exception
|
// but any type can be thrown an as exception
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
// All exceptions thrown inside the _try_ block can be caught by subsequent
|
// All exceptions thrown inside the _try_ block can be caught by subsequent
|
||||||
// _catch_ handlers.
|
// _catch_ handlers.
|
||||||
try {
|
try {
|
||||||
// Do not allocate exceptions on the heap using _new_.
|
// Do not allocate exceptions on the heap using _new_.
|
||||||
throw std::exception("A problem occurred");
|
throw std::runtime_error("A problem occurred");
|
||||||
}
|
}
|
||||||
// Catch exceptions by const reference if they are objects
|
// Catch exceptions by const reference if they are objects
|
||||||
catch (const std::exception& ex)
|
catch (const std::exception& ex)
|
||||||
@ -614,7 +616,7 @@ void doSomethingWithAFile(const char* filename)
|
|||||||
{
|
{
|
||||||
FILE* fh = fopen(filename, "r"); // Open the file in read mode
|
FILE* fh = fopen(filename, "r"); // Open the file in read mode
|
||||||
if (fh == nullptr)
|
if (fh == nullptr)
|
||||||
throw std::exception("Could not open the file.");
|
throw std::runtime_error("Could not open the file.");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
doSomethingWithTheFile(fh);
|
doSomethingWithTheFile(fh);
|
||||||
|
Loading…
Reference in New Issue
Block a user