KEMBAR78
Exception Handling Programs | PDF
0% found this document useful (0 votes)
7 views2 pages

Exception Handling Programs

Uploaded by

gamerteejas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Exception Handling Programs

Uploaded by

gamerteejas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 9: Division By Zero Error

#include <iostream>
using namespace std;

int main() {
int a, b;
float c;
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
try {
if (b != 0)
{
c=a / b;
cout << "Result is: " << c << endl;
}else{
throw "Division by zero error!";
}
}
catch (const char* msg) {
cout << "Error: " << msg << endl;
}
return 0;
}
Program 10: Array out of bound Error
#include <iostream>

#include <stdexcept>

using namespace std;

int main()

int arr[] = {1, 2, 3, 4, 5};

int size = 5;

int index;

cout << "Enter an index to access the array: ";

cin >> index;

try

if (index < 0 || index >= size)

throw out_of_range("Index out of bounds");

else

cout << "Value at index " << index << " is: " << arr[index] << endl;

catch (const out_of_range& e)

cout << "Exception caught: " << e.what() << endl;

return 0;

You might also like