I was making a 2d "safe" array class and was using exceptions if an array out of bounds index was passed. the code fragment would be like so:
Code:
//in array header.
double& Matrix::operator()(unsigned long int r, unsigned long int c){
if(array)
if( (r<n_r)&&(c<n_c) )
return array[(r*n_c) + c];
else
throw"\n\n\t##Array Index Out Of Bounds.\n\n";
else
throw"\n\n\t##Memory NOT Allocated yet.\n\n";
}
//in main().
int main(){
Matrix m(5, 5);
try{
m(5, 5);
}catch(const char* e){
std::cout<<"\n# Exception Caught :"<<e;
}
return 0;
}
My question is that when I didn't type in the 'const' shown in bold, my catch block did'nt catch any exceptions. Why should 'catch' bother whether the string literal thrown was constant or not? It took me a really long time and lot of experimenting untill I got that 'const' in there.
What should I do so that my 'catch' catches all 'char*'s whether 'const' or not?
P.S. This code is for eucational purposes only. so please don't advice me to use vectors or any thing instead of re-inventng the wheel.