赞
踩
type function (argument list) throw (type-list)
{
function body
}
#include <iostream> using namespace std; const int size = 5; template <class T> class vector { T v[size]; public: vector(){} vector(T *b); void show(); T minimum(vector<T> &m); }; template <class T> vector<T>::vector(T *b) { for(int i=0;i<size;i++) v[i] = b[i]; } template<class T> T vector<T>::minimum(vector<T> &m) { int j = 0; for(int k=1;k<size;k++) { if(m.v[j]>m.v[k]) j = k; } return m.v[j]; } template<class T> void vector<T>::show() { cout << "("; for(int t=1;t<size;t++) cout << ", " << v[t]; cout << " )" << endl; } int main() { int x[size] = {5, 7, 3, 1, 8}; float y[size] = {1.2, 1.5, 3.1, 1.1, 0.501}; vector<int> v1; vector<float> v2; v1 = x; v2 = y; cout << "Minimum value = " << v1.minimum(v1) << " of array"; v1.show(); cout << "Minimum value = " << v2.minimum(v2) << " of array"; v2.show(); return 0; }
#include <iostream> #include <cmath> #define PI 3.1416 using namespace std; void power_factor(float a) { if(a>1 || a<-1) throw(a); else cout << "Voltage(V) is lagging from current(I) by : "<< acos(a)*180/PI << "degree\n"; } int main() { float a; try { cout << "Enter power factor "; cin >> a; power_factor(a); } catch(float b) { cout << "Caught an exception \n"; } return 0; }
#include <iostream> using namespace std; void empty() throw() { cout << "In empty()\n"; } void with_type(float x) throw(int) { if(x==1) throw (1); else if (x==1.1) throw(2.1); } int main() { try { empty(); with_type(1); } catch(int n) { cout<< "Caught an int = " << n; } catch(float) { cout<<"Caught a float "; } return 0; }
#include <iostream> using namespace std; void division(int a, int b) { try { if(b==0) throw b; else cout << " a/b = " << (float)a/b << endl; } catch(int) { cout << "Caught an exception as first throwing \n"; throw; } } int main() { int a, b; cout << "Enter the value of a & b : "; cin >> a >> b; try { division(a, b); } catch(int) { cout << "Caught an exception as re-throwing \n"; } return 0;
#include <iostream> using namespace std; long int square(int i) { return i*i; } long int sum(int n) { long int s; s = 0; for(int i=1;i<=n;i++) { s+=square(i); } return s; } void display(int m) { try { if(m<0) throw m; else cout << sum(m)<<endl; } catch(int n) { cout<<"Caught an exception \n"; } } int main() { int n; cout << "Enter a positive number "; cin>>n; display(n); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。