0% found this document useful (0 votes)
6 views5 pages

Exception Handling-01

The document contains multiple C++ code snippets demonstrating exception handling using try-catch blocks. Each code snippet addresses different scenarios such as division by zero, array index access, and invalid input values, throwing exceptions with appropriate messages. The code illustrates how to handle errors gracefully and provide user feedback in various situations.

Uploaded by

abirhasan0033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Exception Handling-01

The document contains multiple C++ code snippets demonstrating exception handling using try-catch blocks. Each code snippet addresses different scenarios such as division by zero, array index access, and invalid input values, throwing exceptions with appropriate messages. The code illustrates how to handle errors gracefully and provide user feedback in various situations.

Uploaded by

abirhasan0033
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

CODE-01 : #include <iostream>

#include<iomanip>
using namespace std;

int main()
{
float x,y;
while(1)
{
try
{

cout<<"The operation is x/y. \n";


cout<<setw(12)<<"Enter x : ";
cin>>x;
cout<<setw(12)<<"Enter y : ";
cin>>y;
if(y==0)
{
throw " y must be non-zero! So division is not possible. \n\n";
}
cout<<" x/y = "<<x<<"/"<<y<<" = "<<x/y<<"\n\n";

}
catch(const char *x)
{
cout<<x;
}
}
return 0;
}

CODE-02: #include <iostream>


#include<iomanip>
using namespace std;

int main()
{
try
{
int n,m,i;
cout<<" Enter the array length : ";
cin>>n;
int a[n];
cout<<" ENTER ARRAY ELEMENTS -\n";
for(i=0;i<n;i++){
cout<<setw(20)<<" Element at index "<<setw(2)<<i<<": ";
cin>>a[i];
}
cout<<" ARRAY ELEMENTS ARE : ";
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<"\n Enter which number of index you want to access : ";
cin>>m;
if(0>m || m>=n){
throw " INVALID INDEX!\n\n";
}
cout<<" You've accessed index "<<m<<" successfully! \n\n";

}
catch(const char *x){
cout<<x;
}
return 0;
}

CODE-03: #include <iostream>


using namespace std;
int main()
{
try
{
throw 3.78;
}
catch(const char *x)
{
cout<<"Error string : "<<x<<endl;
}
catch(int x)
{
cout<<"Error number : "<<x<<endl;
}
catch(...)
{
cout<<"Invalid operation!"<<endl;
}
return 0;
}

CODE-04: #include <iostream>


using namespace std;
int main()
{
try
{
throw string("You've thrown a string");
throw 1;

}
catch(int x)
{
cout<<"You have thrown an integer. It is "<<x<<endl;
}
catch(string x)
{
cout<<x<<endl;
}
return 0;
}

NOW SEE THE CODE ALSO -


#include <iostream>
using namespace std;
int main()
{
try
{

throw 1;
throw string("You've thrown a string");
}
catch(int x)
{
cout<<"You have thrown an integer. It is "<<x<<endl;
}
catch(string x)
{
cout<<x<<endl;
}
return 0;
}

CODE-05: #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x,y;
while(1)
{
try
{
cout<<setw(26)<<"Enter the divisor : ";
cin>>x;
cout<<setw(26)<<"Enter the denominator : ";
cin>>y;
if(y<0 || x<0)
{
throw string(" Negative numbers are not allowed.\n O P E R A
T I O N F A I L E D!\n\n");
}
if(y==0)
{
throw string(" Denominator can't be zero.\n O P E R A T I O N
F A I L E D!\n\n");
}
cout<<" "<<x<<"/"<<y<<" = "<<x/y<<" \n O P E R A T I O N S U C C
E S S F U L!\n\n";
}
catch(string x)
{
cout<<x;
}

}
return 0;
}

CODE-06: #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
while(1)
{
try
{
int n,m;
cout<<"Enter the length of array : ";
cin>>n;
int a[n];
for(int i=0; i<n; i++)
{
cout<<"Element at index "<<setw(2)<<i<<setw(2)<<" : ";
cin>>a[i];
if(a[i]<0)
{
throw string("Negative is not allowed!\nOPERTION FAILED!\n\n");
}
}
cout<<"Enter the index you want to access : ";
cin>>m;
if(m<0 || m>=n)
{
throw string("Array index is not valid!\nOPERATION FAILED!\n\n");
}
cout<<"Element at index "<<m<<" : "<<a[m]<<"\nOPERATION SUCCESSFUL!\n\
n";
}
catch(string s)
{
cout<<s;
}
}
return 0;
}

CODE-07: #include<iostream>
#include<iomanip>
#include<string>

using namespace std;

int main()
{
while(1)
{
try
{
int age;
cout<<"Enter age : ";
cin>>age;
if(age<=0)
{
throw string(to_string(age)+" is an invalid age!\n\n");
}

if(age>120)
{
throw string(to_string(age)+" is an invalid age!\n\n");
}
cout<<"Age : "<<age<<"\n\n";
}
catch(string s)
{
cout<<s;
}
}
return 0;

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