So I have code in Java, now I want that in C++. Everything is fine but I'm having trouble with exceptions.
In the 'Test' class in Java I have
try {
k = Integer.parseInt(args[i]);
}
catch (OutOfRangeException ex) {
System.out.println(args[i]+ ex.getMessage());
}
This is 'OutOfRange.java'
public class OutOfRangeException extends Exception {
public OutOfRangeException (String message) {
super (message);
}
}
And method from another class that throws that exception
public int number (int m) throws OutOfRangeException {
if (m < 0 || m >= arr.length) {
throw new OutOfRangeException(" - number out of range");
}
return arr[m];
}
};
I can't get the same effect with C++, I read a lot about it but still it doesn't work (I get 'dynamic exception specifications are deprecated in C++11' when it comes to 'throw' but I don't know how to get message in C++ either).
Is there a way I can do this like in Java?
I tried something like this: In main:
try {
k = stoi(argv[i]);
}
catch (OutOfRangeException &e) {
cout << argv[i]<< endl;
}
Method:
int PrimeNumbers: number (int m) throw (OutOfRangeException) {
if (m < 0 || m >= sizeof(arr)) {
throw OutOfRangeException(" - number out of range");
}
return arr[m];
}
and constructor of OutOfRangeException
OutOfRangeException::OutOfRangeException(string message) {
cout<<message;
}
The thing is I get this 'dynamic exception...' error and I haven't found any other way to do it so I would be satisfied with it.