Question

In: Computer Science

Exceptional task in c++ OOP: 1. Create three exceptions classes / structures, for division by zero,...

Exceptional task in c++ OOP: 1. Create three exceptions classes / structures, for division by zero, first second for out of range, third for reading file that doesn't exist. Secure given code with self-made exceptions

Solutions

Expert Solution

Hi,

Please find the code below :

***************************************************************************

1.) For Division By Zero:-

Code:-

#include <iostream>

#include <stdexcept>

#include <bits/stdc++.h>

using namespace std;

//handling divide by zero

float Result(float input, float divisor){

if (divisor == 0) {

throw runtime_error("Math error: Attempted to divide by Zero\n");

}

return (input / divisor);

}

//Driver Function

int main(){

float divident, divisor, result;

divident = 14.5;

divisor = 0;

try {

result = Result(divident, divisor);

cout << "The quotient is " << result << endl;

}

catch (runtime_error& e) {

cout << "Exception occurred" << endl << e.what();

}

}

Output:-

******************************************************************************

2) For Out of Range:-

Code:-

#include <iostream>

#include <stdexcept>

#include <vector>

#include <bits/stdc++.h>

using namespace std;

int main () {

std::vector<int> example(10);

try {

example.at(15) = 100;

} catch (const std::out_of_range& oor) {

std::cerr << "Out of Range error: " << oor.what() << '\n';

}

return 0;

}

Output:-

******************************************************************************

3) For File Does not Exist:-

Code:-

#include <stdio.h>

#include <errno.h>

#include <string.h>

extern int gabcd ;

int main () {

FILE * pfile;

int abcd;

pfile = fopen ("donotexist.txt", "rb");

  

if (pfile == NULL) {

abcd = gabcd;

fprintf(stderr, "Value of errno: %d\n", gabcd);

perror("Error printed by perror");

fprintf(stderr, "Error opening file: %s\n", strerror( abcd ));

} else {

fclose (pfile);

}

return 0;

}

Output:-

******************************************************************************

Please contact me if you need more clarification.

Thank you :)


Related Solutions

Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with...
Practice Coding Task C++ ATM Machine with Some Classes Create an ATM machine in C++ with a few classes. Requirements: Automated Teller Machine (ATM) simulationGiven 3 trials, the user is able to see his balance by entering a four-digit pin that must NEVER be displayed on screen but masked by the Asterix (*) character. A list of pins stored on the file system must be loaded to verify the pin. The user should be able to withdrawfundsbelow a set limit...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes:...
Task 1. Create class "Vehicle" with variables "company" and "year". LM 2. Create 3 derived classes: "Bus", "Car", and "Motorcycle". They should contain a variable storing number of seats (bus) / weight (car) / number of tires (motorcycle), constructor, destructor and a print_info function that will print all the information about the vehicle. PK 3. The constructors should take as parameters: company, year and number of seats / weight / number of tires. Inside it assign the company name and...
Modify the BankAccount class to throw IllegalArgumentException exceptions, create your own three exceptions types. when the...
Modify the BankAccount class to throw IllegalArgumentException exceptions, create your own three exceptions types. when the account is constructed with negative balance, when a negative amount is deposited, or when an amount that is not between 0 and the current balance is withdrawn. Write a test program that cause all three exceptions occurs and catches them all. /** A bank account has a balance that can be changed by deposits and withdrawals. */ public class BankAccount { private double balance;...
C++ Assignment 1: Make two classes practice For each of the two classes you will create...
C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Task use c++ and Create Base class task with virtual method Create a pair of derivative...
Task use c++ and Create Base class task with virtual method Create a pair of derivative classes of architecture, scientist, economist - Define a salary in each of these classes and create a method that prints the salary for each job Create a working object Use dynamic_cast Use typeid Create an object for each job that it will include the number of employees of that type and the method of printing these numbers
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with...
(MUST BE DONE IN C (NOT C++)) In this task, you will create a structure with arrays. You will have to create your own structure. However, make sure to meet these guidelines: - Give the structure whichever name you want. - It must have at least 3 members. - Two of the members must be arrays. - Your members should be of at least two different data-types. In other words, your members cannot be integers only (or floats, or doubles…)....
C++ Classes & Objects Create a class named Student that has three private member: string firstName...
C++ Classes & Objects Create a class named Student that has three private member: string firstName string lastName int studentID Write the required mutator and accessor methods/functions (get/set methods) to display or modify the objects. In the 'main' function do the following (1) Create a student object "student1". (2) Use set methods to assign StudentID: 6337130 firstName: Sandy lastName: Santos (3) Display the students detail using get functions in standard output using cout: Sandy Santos 6337130
Create a C++ program to simulate an art gallery: 4 classes: Gallery class Has three vectors...
Create a C++ program to simulate an art gallery: 4 classes: Gallery class Has three vectors of type Painting which represent three categories of paintings (abstract, impressionism, pointillism) Has a function that reads in paintings from a file and stores each painting in the correct vector Read in paintings from file (will have multiple paintings) Title of painting Artists first name Artists last name Address street number Address street name Address city Address state Address zip Artists website Category Number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT