a) List common examples of exceptions and explain how exceptions are handles in c++. (try, catch, throw)
b) Declare an abstract class Fraction that can be used for arithmetic of the * operator.
a) Common examples of exceptions :
Exception Handling
#include <iostream>
using namespace std;
int main()
{
int hours,minutes,seconds;
string amOrPm;
do //loop to input hours until hours <0 or hours > 12
{
try
{
cout<<"\nEnter hours: ";
cin>>hours;
if(hours <0 || hours >12)
throw hours; // throw hours exception
else
break;
}catch(int hours) //catch exception and display message
{
cout<<"\nThe value of hr must be between 0 and 12.";
}
}while(hours<0 || hours >12);
do //loop to input minutes until minutes <0 or minutes >
59
{
try
{
cout<<"\nEnter minutes: ";
cin>>minutes;
if(minutes <0 || minutes >59)
throw minutes; // throw minutes exception
else
break;
}catch(int minutes) //catch exception and display message
{
cout<<"\nThe value of minutes must be between 0 and
59.";
}
}while(minutes<0 || minutes>59);
do //loop to input seconds until seconds <0 or seconds >
59
{
try
{
cout<<"\nEnter seconds: ";
cin>>seconds;
if(seconds <0 || seconds > 59 )
throw seconds; // throw seconds exception
else
break;
}catch(int seconds) //catch exception and display message
{
cout<<"\nThe value of seconds must be between 0 and
59.";
}
}while(seconds<0 || seconds >59);
cout<<"\nEnter AM or PM: ";
cin>>amOrPm;
if(amOrPm == "PM")
{
hours = hours +12;
}
cout<<"\n24 hour clock time: ";
cout<<hours<<":"<<minutes<<":"<<seconds;
return 0;
}
Output:
Enter hours: 14
The value of hr must be between 0 and 12.
Enter hours: -2
The value of hr must be between 0 and 12.
Enter hours: 5
Enter minutes: 70
The value of minutes must be between 0 and 59.
Enter minutes: 30
Enter seconds: -2
The value of seconds must be between 0 and 59.
Enter seconds: 45
Enter AM or PM: AM
24 hour clock time: 5:30:45
b)
class Fraction
{
private:
int numerator,denominator;
public:
Fraction(int numerator,int denominator);//constructor
virtual Fraction operator*(Fraction f); // abstract operator
overloading function makes the class abstract
};
Do ask if any doubt. Please upvote.
a) List common examples of exceptions and explain how exceptions are handles in c++. (try, catch,...
Java Programming
Q4 a. List five common examples of exceptions b. What is "resource leak" c. What happens if several catch blocks match the type of thrown object? d. What is the key reason for using finally blocks?
I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...
C++ Your assignment this week is to make your fractionType class safe by using exception handling. Use exceptions so that your program handles the exceptions division by zero and invalid input. · An example of invalid input would be entering a fraction such as 7 / 0 or 6 / a · An example of division by zero would be: 1 / 2 / 0 / 3 Use a custom Exception class called fractionException. The class must inherit from exception...
Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...
1) In C++, come up with a scenario where you should use a catch(...). What should you do in that catch block? 2) Describe in detail any problems you see in the following code. void funcOne() { Dog *ptr = new Dog; cout << “Presenting the result of a division: “ << funcDivide(); delete ptr; } int funcDivide() { int a, b; cin >> a >> b; if (b == 0) throw “Divide...
Hello
these are midterm study guide questions for my
C++ class. I need some clarification on how to
understand and write these functions.
Can someone write out examples and use definitions on how to go
about the stuff listed below?
again this is for an introductory C++ computer science class
Functions Predefined Functions Know basic predefined functions covered from <cmath> Know how to use the rand) function from <stdlib.h> Be prepared to convert arithmetic expressions to function calls. Programmer Defined...
12p
I need help this is Python
EXCEPTIONS: It's easier to ask forgiveness than permission. Try the code and catch the errors. The other paradigm is 'Look before you leap' which means test conditions to avoid errors. This can cause race conditions. 1.Write the output of the code here: class MyError(Exception): pass def notZero(num): if num == 0: raise MyError def run(f): try: exec(f) except TypeError: print("Wrong type, Programmer error") except ValueError: print("We value only integers.") except Zero Division Error:...
What is dynamic data type in C#? How to declare a dynamic data type? Describe in detail the role of Common Language Runtime (CLR) in .NET. Describe abstract classes in C# with an example. List and explain Overloadable and Non-Overloadable Operators in C#. Explain any two ways of passing parameters to a method in C#.
Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...