Exceptions
std::stoi("b");
vector<int> vecOfInts;
void SetsToFive(int& x)
{
x = 5;
}
/**************************/
int n = 10;
SetsToFive(x);
std::cout << n << std::endl;
std::ifstream myFile;
myFile.open("does_not_exist.txt");
std::vector<std::string> someVec;
someVec.size()
void SetsToBlank(std::string& x)
{
x = "";
}
/**************************/
std::string n = "something";
SetsToBlank(n);
std::cout << n << std::endl;
std::vector v; v.push_back(5639); v.at(1);
std::vector<std::string> teamList;
teamList.push_back("example1");
teamList.push_back("example2");
void MayThrowException(int& x)
{
if (x < 0)
{
throw std::exception("x is less than 0");
}
}
int main()
{
try
{
int n = -5;
MayThrowException(n);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
try
{
std::cout << "hi there" << std::endl;
} catch(std::exception e)
{
std::cerr << "ERROR" << std::endl;
}
hi there
std::vector<int> vecOfInts; int firstInt = vecOfInts.at(1);
void SetsBothToZero(int& x, int y)
{
x = 0;
y = 0;
}
/******************************/
int a = 6;
int b = 6;
SetsBothToZero(a, b);
std::cout << "a=" << a << ",b=" << b << std::endl;
void ThrowsAnException()
{
std::vector v;
v.at(0);
std::cout << "I just tried to access an invalid location"<< std::endl;
}
static void Main(string[] args)
{
try
{
ThrowsAnException();
}
catch(std::out_of_range& e)
{
std::cout << "ERROR: Invalid access" << std::endl;
}
}
ERROR: Invalid access
std::vector<int> vecOfInts;
vecOfInts.push_back(3);
vecOfInts.push_back(2);
vecOfInts.push_back(1);
for(int i : vecOfInts)
{
std::cout << i << std::endl;
}
void doSomethingCrazy(int n)
{
for (int i = 0; i < n; ++i)
{
if (i < 10)
{
throw std::runtime_error("bad i=" + std::to_string(i));
}
}
}
int main(int argc, char* argv[])
{
int n = 10;
try
{
doSomethingCrazy(n);
}
catch (std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
}