Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //handle divided by 0 exception
- #include<iostream>
- #include<exception>
- using namespace std;
- int main()
- {
- int a,b;
- cout<<"\n Enter the value of a & b:";
- cin>>a>>b;
- int x=a-b;
- try
- {
- if(b!=0)
- cout<<"\n Result (a/x) = "<<a/x<<endl;
- else
- throw(x);
- }
- catch(int i)
- {
- cout<<"\n Exception caught: DIVIDE BY ZERO"<<endl;
- }
- cout<<"\n END";
- return 0;
- }
- // first alphabet in not a vowel throw exception
- #include<iostream>
- #include<exception>
- using namespace std;
- int main()
- {
- char *p;
- cout<<"\n Enter a srtring(lowercase):";
- cin>>p;
- try
- {
- if((p[0]=='a')||(p[0]=='e')||(p[0]=='i')||(p[0]=='o')||(p[0]=='u'))
- cout<<"\n "<<p<<endl;
- else
- throw(p[0]);
- }
- catch(char c)
- {
- cout<<"EXCEPTION CAUGHT not a vowel";
- }
- return 0;
- }
- // input aplhanumeric string if first character is not an alphabet throw an exception
- #include<iostream>
- #include<exception>
- #include<ctype.h>
- using namespace std;
- int main()
- {
- char *p;
- cout<<"\n Enter an alphanumeric string:";
- cin>>p;
- try
- {
- if(!isalpha(p[0]))
- throw(p[0]);
- else
- cout<<p<<endl;
- }
- catch(char c)
- {
- cout<<"EXCEPTION CAUGHT not an alphabet";
- }
- return 0;
- }
- // 4th program
- #include<iostream>
- #include<exception>
- #include<ctype.h>
- #include<string.h>
- using namespace std;
- int main()
- {
- char p[20];
- cout<<"\n Enter an alphanumeric string:";
- cin>>p;
- try
- {
- if(!isalpha(p[0]))
- throw(p[0]);
- if(isalpha(p[2]))
- throw 4;
- if(p[strlen(p)-1]!='0')
- throw 1.0;
- else
- cout<<p<<endl;
- }
- catch(char c)
- {
- cout<<"EXCEPTION CAUGHT not an alphabet";
- }
- catch(int i)
- {
- cout<<"\n EXCEPTION CAUGHT not an integer";
- }
- catch(double f)
- {
- cout<<"\n EXCEPTION CAUGHT last character not a zero";
- }
- cout<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement