61.9k views
5 votes
. What happens if an exception is thrown by a class s member function?

User Tom Knapen
by
7.5k points

1 Answer

4 votes
Likely a runtime error. If it works in your scenario, you can try a block of code, and catch any exceptions to fix them or display an error message.

You did not mention what language you're using, so here's an example in C++


#include <iostream>
int main()
{
int a, b;

std::cout << "Enter two numbers to divide: ";
std::cin >> a >> b;

try
{
if (b == 0)
throw "Cannot divide by zero!";
else
std::cout << a << "/" << b << "=" << a / b;
}
catch (std::string errMsg)
{
std::cout << errMsg;
}

return 0;
}