Question

In c++ code and test a function with prototype float except(int arg); that accepts an int...

In c++ code and test a function with prototype float except(int arg); that accepts an int argument and returns the expression 1.0 / arg if arg != 0; otherwise, except() throws an exception of type string. A suitable message might be “Error – attempt to divide by zero”.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Hi first of all let us Discuss about the Exception
As w know The Exception is nothing but Runtime Error occur at Runtime.that affects the Current Execution of a process
whih is abnormally terminates the certain Process,Like is an Object that wraps an Error Event that occured within a function.
To handle these type of Exceptions, we need apply apply the try followed by catch block.
Exception Handling is a Mechanism useful for handling the Runtime Errors.

->Actually an Error code Containing that block should be Kept in in Try Block.
Try Block will immediately follows the Certain Catch Block to catch the Exception that occur at Runtime.
Advantages of Exception Handling:-
==================================
The Main purpose of Using Exception Handling is to Error Detectio and Correction.
->Separating Error-Handling Code from "Regular" Code
-> We can Easily Identifying the Errors in a Program.

for Example, An array Containing the List/Sequence of an identical elements.Actually it accepts only a similar type of Elements.
Suppose Even if we enter Wrong input or Different.The Controller will continue to take input from the user.
Now we will go through your requirement,
please follow the below steps to implement to handle that type Exception.

c++ souce code:-
=================
#include<iostream>
using namespace std;
float except(int arg);
int main()
{
int arg;
float result;
cout<<"\n*** Exception demo ****"<<endl;
cout<<"\nPlease Enter any Integer value: ";
cin>>arg;
try
{
result=except(arg);
cout<<"The Return Value is"<<result<< endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}
return 0;
}
float except(int arg)
{
if(arg!=0)
return 1.0;
else
throw "Error – attempt to divide by zero";
}
sample outputs:-
===================

*** Exception demo ****
Please Enter any Integer value: 12
The Return Value is1
--------------------------------
Process exited after 5.317 seconds with return value 0
Press any key to continue . . .





*** Exception demo ****
Please Enter any Integer value: 0
Error û attempt to divide by zero
--------------------------------
Process exited after 1.873 seconds with return value 0
Press any key to continue . . .



Another Example Source code:-
==============================
#include <iostream>
using namespace std;
class Exception
{
public:
Exception(string name)
{
}
public:
double division(int a, int b)
{
if(b==0)
{
throw "Exception:Division by zero condition!";
}
return (a/b);
}
public:
double multiplication(int a, int b)
{
if(a==0||b==0 )
{
throw "Exception:Multiply by zero condition!";
}
return (a*b);
}
public:
double Addition(int a, int b)
{
if(a>=255||b>=255)
{
throw "Exception:addition by Leading Value condition!";
}
return (a+b);
}
public:
double substraction(int a, int b)
{
if(a<0||b<0)
{
throw "Exception:Sustract by Negtive Value condition!";
}
return (a-b);
}
};
int main ()
{
Exception obj("Exception Handling Demonstration");
int n1,n2,opt;
double result=0;
try
{
while(1)
{
cout<<"------------------------------"<<endl;
cout<<"*******Exception Handling******"<<endl;
cout<<"1.Addition Of Numbers:"<<endl;
cout<<"2.Multiplication of Two Numbers:"<<endl;
cout<<"3.subtraction of Two Numbers:"<<endl;
cout<<"4.Division of Two Numbers:"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter Any Two Numbers"<<endl;
cin>>n1>>n2;
cout<<"Select Any Operation:"<<endl;
cin>>opt;
switch(opt)
{
case 1:
try
{
result=obj.Addition(n1,n2);
cout <<"The Addition Value is"<<result<< endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}
break;
case 2:
try
{
result=obj.multiplication(n1,n2);
cout <<"The Result is:"<<result<< endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}
break;
case 3:
try
{
result=obj.substraction(n1,n2);
cout <<"The Result is:"<<result<< endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}
break;
case 4:
try
{
result=obj.division(n1,n2);
cout <<"The Result is:"<<result<< endl;
}
catch (const char* msg)
{
cerr << msg << endl;
}
break;
case 5:
exit(0);
default:
cout<<"invalid Option"<<endl;
}

}
}
catch(const char*msg)
{
cout<<"Exception caught"<<endl;
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In c++ code and test a function with prototype float except(int arg); that accepts an int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  •    PYTHON --create a function that accepts a list of numbers and an int that returns...

       PYTHON --create a function that accepts a list of numbers and an int that returns index of 2nd occurrence of the int in list, otherwise returns None if # does not repeat more 2 or more times EX: [10,24,3,45,10,49,4,5], 10) returns 4 --create a function that accepts a string that returns true if every letter of the alphabet can be found at least one time in the string, (has to be the lowercase alphabet), and false otherwise.    for...

  • you need to write a C function named rem. The prototype is: int rem(char*, char*); The...

    you need to write a C function named rem. The prototype is: int rem(char*, char*); The function accepts 2 strings: the first is a string of text to process; the second is where the output will be placed. rem() should remove all occurrences of a lowercase 'x' from the first string, and save the resulting string in the second arg. You may use the strlen() function; no other built-in fuctions should be used. Test your program thoroughly. Then, once you...

  • C++ clock.h /* Write a function get_hours that returns an int and accepts an int seconds_since_1970...

    C++ clock.h /* Write a function get_hours that returns an int and accepts an int seconds_since_1970 parameter */ int get_hours(int seconds_since_1970); /* Write a function get_minutes that returns an int and accepts an int seconds_since_1970 parameter */ int get_minutes(int seconds_since_1970); /* Write a function get_seconds that returns an int and accepts an int seconds_since_1970 parameter */ int get_seconds(int seconds_since_1970); clock.cpp #include "clock.h" /* Write get_hours code to return hours given seconds since 1970 int */ /* Write get_minutes code to...

  • Write code for a function with the following prototype: // Divide by power of two. Assume...

    Write code for a function with the following prototype: // Divide by power of two. Assume 0 <= k < w-1 int divide_power2(int x, int k); The function should compute x/2 k with correct rounding (round toward 0), and it should follow the bit-level integer coding rules: • Assumptions – Integers are represented in twos-complement form. – Right shifts of signed data are performed arithmetically. – Data type int is w bits long. For some of the problems, you will...

  • Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float...

    Language: Python Topic: Try/Except Function name : add_divide Parameters : list of ints, int Returns: float Description: Given a list of integers and a number, you want to add the numbers in the list to a total value by iterating through the given list. However, when the index you are at is divisible by the integer passed in, you must instead divide the current total by the element at that index. You must be careful when dividing the total (you...

  • Must be in C. 2. Show code for function, appendArray, based on the following prototype: int...

    Must be in C. 2. Show code for function, appendArray, based on the following prototype: int appendArray(int array1M[], int iCounti,int array2M[], int iCount2, int iMaxArray1) This function should take the contents of array2M (which has iCount2 elements) and append it onto the end of array1M (which originally has iCount1 elements). The maximum number of elements in array1M should be iMaxArray1. appendArray also returns the new count of items in array1M. Example: array1M 11 12 13 iCount1 3 iMaxArrayl8 array2M 24...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and...

    Write a Python program which defines a function named "divide_two_numbers" that accepts two numeric parameters and divides the first parameter by the second. Your divide_two_numbers function must check the second parameter before dividing; if it is zero, raise an ZeroDivisionError exception which includes the string "second parameter was zero!" as a parameter instead of processing the division. Your main function must call your divide_two_numbers function at least twice; one call must use two normal parameters to verify normal operation, the...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    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...

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT