Advertisement
Guest User

oop lab 21/10

a guest
Oct 20th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. //handle divided by 0 exception
  2.  
  3. #include<iostream>
  4. #include<exception>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.    
  11.     int a,b;
  12.     cout<<"\n Enter the value of a & b:";
  13.     cin>>a>>b;
  14.     int x=a-b;
  15.     try
  16.     {
  17.         if(b!=0)
  18.             cout<<"\n Result (a/x) = "<<a/x<<endl;
  19.    
  20.         else
  21.            
  22.             throw(x);
  23.     }
  24.  
  25.     catch(int i)
  26.     {
  27.         cout<<"\n Exception caught: DIVIDE BY ZERO"<<endl;
  28.     }
  29.     cout<<"\n END";
  30.  
  31.     return 0;
  32. }
  33.  
  34.  
  35. // first alphabet in not a vowel throw exception
  36.  
  37. #include<iostream>
  38. #include<exception>
  39.  
  40. using namespace std;
  41. int main()
  42. {
  43.  
  44.     char *p;
  45.     cout<<"\n Enter a srtring(lowercase):";
  46.     cin>>p;
  47.  
  48.     try
  49.     {
  50.         if((p[0]=='a')||(p[0]=='e')||(p[0]=='i')||(p[0]=='o')||(p[0]=='u'))
  51.             cout<<"\n "<<p<<endl;
  52.  
  53.         else
  54.             throw(p[0]);
  55.     }
  56.     catch(char c)
  57.     {
  58.         cout<<"EXCEPTION CAUGHT not a vowel";
  59.     }
  60.    
  61.     return 0;
  62. }
  63.      
  64.    
  65.  
  66. // input aplhanumeric string if first character is not an alphabet throw an exception
  67.  
  68. #include<iostream>
  69. #include<exception>
  70. #include<ctype.h>
  71.  
  72. using namespace std;
  73.  
  74. int main()
  75. {
  76.  
  77.     char *p;
  78.     cout<<"\n Enter an alphanumeric string:";
  79.     cin>>p;
  80.    
  81.     try
  82.     {
  83.         if(!isalpha(p[0]))
  84.             throw(p[0]);
  85.        
  86.         else
  87.             cout<<p<<endl;
  88.     }
  89.  
  90.     catch(char c)
  91.     {
  92.         cout<<"EXCEPTION CAUGHT not an alphabet";
  93.     }
  94.     return 0;
  95. }
  96.  
  97.  
  98. // 4th program
  99.  
  100. #include<iostream>
  101. #include<exception>
  102. #include<ctype.h>
  103. #include<string.h>
  104.  
  105. using namespace std;
  106.  
  107. int main()
  108. {
  109.  
  110.     char p[20];
  111.     cout<<"\n Enter an alphanumeric string:";
  112.     cin>>p;
  113.    
  114.     try
  115.     {
  116.         if(!isalpha(p[0]))
  117.             throw(p[0]);
  118.         if(isalpha(p[2]))
  119.             throw 4;
  120.         if(p[strlen(p)-1]!='0')
  121.             throw 1.0;
  122.        
  123.         else
  124.             cout<<p<<endl;
  125.     }
  126.  
  127.     catch(char c)
  128.     {
  129.         cout<<"EXCEPTION CAUGHT not an alphabet";
  130.     }
  131.     catch(int i)
  132.     {
  133.         cout<<"\n EXCEPTION CAUGHT not an integer";
  134.     }
  135.     catch(double f)
  136.     {
  137.         cout<<"\n EXCEPTION CAUGHT last character not a zero";
  138.     }
  139.  
  140.     cout<<endl;
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement