0% found this document useful (0 votes)
8 views1 page

UserDefinedException-4

The C++ program prompts the user to enter a number and checks if it is odd or even. If the user inputs a negative number, it raises a user-defined exception called 'Negative Number'. The program handles exceptions and displays appropriate messages based on the user's input.

Uploaded by

dh4nhwbgqm
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)
8 views1 page

UserDefinedException-4

The C++ program prompts the user to enter a number and checks if it is odd or even. If the user inputs a negative number, it raises a user-defined exception called 'Negative Number'. The program handles exceptions and displays appropriate messages based on the user's input.

Uploaded by

dh4nhwbgqm
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/ 1

Write a C++ program to get a number from the user and find whether the given number is odd or

even. If the user enters the negative number, raise the user defined exception “Negative Number”

#include <iostream>
#include <exception>
using namespace std;
class MyException : public exception{
public:
const char * what() const throw()
{
return "Negative Number!\n";
}
};
int main()
{
try
{
int x;
cout << "Enter the number : \n";
cin >> x;
if (x< 0)
{
MyException z;
throw z;
}
else
{
if(x%2==0)
{
cout <<x<<" is Even Number" << endl;
}
else
{
cout <<x<<" is Odd Number" << endl;
}
}
}
catch(exception& e)
{
cout << e.what();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy