Usually, return value can represent if the function is performed successfully (use '1' and '0'), and then we can use "if" sentence to verify the return value. To avoid any exception, we just need use return before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Example: what is the condition to use either of them?
int set(int num){
if(num<0)
return 0;
....
}
int set(int num){
try{
if(num<0)
throw 0;
}
catch ....
....
}
I think exceptions are more for internals errors, such as bad memory allocations or failure to read a file. Personally, I never use them and instead use cout's and manual debugging to see if something could go wrong. Then again, I never do anything that can go wrong for reasons besides my own stupidity causing numbers to have illegal values.
Using exceptions allow you to include information about what exactly went wrong. When you are working together on a project with others who need to be able to call the function this might provide more clarity.
Also exception give you the potential to treat a situation with a catch clause and if needed (unexpected error etc) to pass the exception to outer level. For example the different exception that can be thrown can be handled differently.
Anyway I think exception provides flexibility that return value of functions cannot: they stop the program when throw.