You are on page 1of 6

CODE: CPP.Assignment11.

Opt1 FRESHER
Assignment topic: Exception Handling
ACADEMY
Assignment duration: 120 minutes

Bài 1: Viết các hàm sau:

1. Hàm bất kỳ sử dụng try, catch, throw


#include <math.h>
#include <iostream>

using namespace std;


int main()
{
cout << "Nhập số: ";
double x;
cin >> x;

try
{

if (x < 0.0)
throw "Không thể lấy hàm sqrt của số âm";

cout << "Hàm sqrt của " << x << " là " << sqrt(x) << '\n';
}
catch (const char* exception)
{
cout << "Error: " << exception << '\n';
}
}

2. Hàm bất kỳ sử dụng Multiple catch blocks


#include <iostream>
#include <exception>
using namespace std;

int main() {
try
{
int* myarray = new int[1000];
cin >> *myarray;
}
catch (exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
catch (length_error& le)
{
cout << "Length error" << le.what() << '\n';
}
return 0;
}

3. Hàm bất kỳ sử dụng nested exception


#include <iostream>
#include <exception>
#include <stdexcept>

void print_what(const std::exception& e) {


std::cerr << e.what() << '\n';
try {
std::rethrow_if_nested(e);
}
catch (const std::exception& nested) {
std::cerr << "nested: ";
print_what(nested);
}
}

void throw_nested() {
try {
throw std::logic_error("first");
}
catch (const std::exception& e) {
std::throw_with_nested(std::logic_error("second"));
}
}

int main() {
try {
throw_nested();
}
catch (std::exception& e) {
print_what(e);
}

return 0;
}

Bài 2: Điền vào chỗ trống:

1) Điền vào _____ để output là “Division by zero not possible”

#include <iostream>

using namespace std;

int main()

int a = 10, b = __0__ , c;


try

if (b == 0)

throw "_____ Division by zero not possible ______________";

c = a / b;

catch (_const char* ex_____)

cout << ex;

return 0;

2) Điền vào _____ để output là: “Special exception”

“Special exception”

#include <iostream>

using namespace std;

int main()

int x[3] = { -1,2 };

for (int i = 0; i < 2; i++)

{
int ex = x[i];

try

if (_ex != x[i]____)

throw ex;

else

throw 'ex';

catch (_...___)

cout << "Special exception\n";

return 0;

3) Điền vào _____ để output là: “X”

#include <iostream>
#include <exception>
using namespace std;
int main() {
long long i = _-1____;
try
{
char* text = __new char[i]__;
}
catch (bad_alloc& e)
{
cout << "X";
}
catch (exception& e)
{
cout << "Y";
}
catch (...)
{
cout << "Z";
}
return 0;
}

Bài 4:

#include <string.h>
#include <iostream>
using namespace std;

int dem(int n) {
int sum = 1;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

int main() {
int n;
cin >> n;
cout << dem(n);
return 0;
}

Bài 5:

#include <iostream>

using namespace std;

struct toado {
double x;
double y;
void khoitao() {
cin >> x >> y;
}
};

double khoangcach(double x1,double x2, double y1, double y2) {

return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);


}
void kiemtra( double a, double b, double c) {

if (sqrt(a) < sqrt(b) + sqrt(c) || sqrt(b) < sqrt(a) + sqrt(c) || sqrt(c) <
sqrt(a) + sqrt(b))
{
if (a == b + c || b == a + c || c == a + b)
{
if (a == b || b == c || c == a)
cout << "Day la tam giac vuong can";
else cout << "Day la tam giac vuong";
}
else if (a == b && b == c)
cout << " Day la tam giac deu";
else if (a == b || b == c || c == a)
cout << " Day la tam giac can";
else if (a * a > b * b + c * c || b * b > c * c + a * a || c * c > a * a +
b * b)
cout << " Day la tam giac tu";
else
cout << "Day la tam giac nhon";
}
else
cout << " 3 diem nay khong tao nen tam giac";
}

int main() {
toado a, b, c;
a.khoitao();
b.khoitao();
c.khoitao();

double ab = khoangcach(a.x, a.y, b.x, b.y);


double ac = khoangcach(a.x, a.y, c.x, c.y);
double bc = khoangcach(b.x, b.y, c.x, c.y);
kiemtra(ab, bc, bc);
return 0;
}

You might also like