-
In below mention code when I call “method” with
floatargument, it automatically cast tointand perform the necessary task. But when if I throwfloattype and the immediatecatchexpectsintargument, it is not working? why? -
Another thing, if there is no
catchstatement withfloatthen should it go to generalcatchand from there if I re-throw whichcatchwill handle it?int method(int i) { return i--; } void main() { try { cout<<method(3.14); throw string("4"); } catch(string& s){ try{ cout << s; throw 2.2; } catch(int i) cout<<i; catch(...) throw; cout<<"s"+s; } catch(...) cout<<"all"; }
In below mention code when I call method with float argument, it automatically cast
Share
The function call is resolved at compile time, where the compiler is able to check the types, find the closest match (overload resolution) and then do the appropriate conversion. No such things happen in runtime when an exception is propagated. The exception is caught by a
catchthat matches the type exactly, or one of the exception’s unambiguous bases. In your caseintsimply does not match adouble.As with your second problem: your
rethrowis not enclosed by atryblock, so it is not caught by the lastcatch(...). The lastcatch(...)corresponds to the firsttryblock.