Is it bad practice use try-catch like goto?
For example, simple code
try{
if(argc<2){
std::cout<<"no inputfile"<<std::endl;
throw 1;
}
STARTUPINFO cif;
ZeroMemory(&cif,sizeof(STARTUPINFO));
PROCESS_INFORMATION pi;
if(FALSE==CreateProcess(argv[1],NULL,NULL,NULL,FALSE,NULL,NULL,NULL,
&cif,&pi)){
printf("smth is wrong");
throw 1;
}
WaitForSingleObject(pi.hProcess, INFINITE);
GetExitCodeProcess(pi.hProcess, &exitCode);
std::cout<<"Process return"<<exitCode<<std::endl;
throw 1;
}
catch(int a){
printf("press Enter");
getchar();
}
If you’re asking whether it’s wrong to use exceptions for program flow, the answer is yes, it’s wrong.
That said, for cases where you don’t care about performance too much, you can get away with it as long as you don’t tell anyone on the internet, and as long as you don’t have a production requirement or a long-term maintenance requirement.
In this instance you appear to be using exceptions correctly for everything except the final, successful case.