Let A is an integer array of size 10 and array A can only accept 10 integers. Check any negative number is found and array out of bound if size of array exceeds. If found throw an exception.
Runtime Input :
10
25
-15
30
22
Output :
Negative Number Exception Caught
#include <iostream>
using namespace std;
int main(){
int input[10], n;
try{
cout<<"How many elements do you want to input? ";
cin>>n;
string s = "Array out of bounds Exception Caught";
if(n > 10) throw(s);
else{
cout<<"Input elements\n";
for(int i = 0; i < n; i++){
cin>>input[i];
}
s = "Negative Number Exception Caught";
for(int i = 0; i < n; i++){
if(input[i] < 0) throw(s);
}
}
}
catch(string err){
cerr<<err;
}
return 0;
}
Comments
Leave a comment