I am trying to catch this error but this code does not work. I tried to add different catch conditions but none seem to work. Is it possible to catch this error?
#include <iostream>
#include <exception>
#include <stdexcept>
void myFunction(int* ptr) {
int y = 3;
}
int main() {
try {
int x = 5;
myFunction(x); // call function with invalid parameter
}
catch (const std::invalid_argument& ia) {
std::cerr << "Invalid argument: " << ia.what() << '\n';
}
catch(const std::exception& e) {
std::cerr << "Invalid conversion error: " << e.what() << '\n';
}
catch(...) {
std::cerr << "Error" << '\n';
}
return 0;
}
Error message:
main.cpp: In function ‘int main()’:
main.cpp:20:16: error: invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
20 | myFunction(x); // call function with invalid parameter
| ^
| |
| int
main.cpp:13:22: note: initializing argument 1 of ‘void myFunction(int*)’
13 | void myFunction(int* ptr) {
| ~~~~~^~~