programming-examples/c++/Others/An Example of Exception Handling.cpp

40 lines
722 B
C++
Raw Normal View History

2019-11-15 12:59:38 +01:00
An Example of Exception Handling
#include <iostream>
using namespace std;
int main()
{
unsigned int TypeOfLoan = 0;
const char *LoanType[] = { "Personal",
"Car",
"Furniture",
"Musical Instrument",
"Boat" };
try {
cout << "Enter the type of loan\n";
for(int i = 0; i < 4; i++)
cout << i + 1 << ") " << LoanType[i] << endl;
cout << "Your choice: ";
cin >> TypeOfLoan;
if( TypeOfLoan < 1 || TypeOfLoan > 5 )
throw "Number out of range\n";
cout << "\nType of Loan: " << LoanType[TypeOfLoan] << endl;
}
catch(const char* Msg)
{
cout << "Error: " << Msg << endl;
}
catch(...)
{
cout << "\nSomething went wrong\n\n";
}
return 0;
}