0

Exception handling in C++

In my below code, why doesn't the catch block catch my "int" exception? https://code.sololearn.com/cphMlBbGvbLj/?ref=app

8th Aug 2021, 2:12 AM
Rishi
Rishi - avatar
4 Antworten
+ 2
Rishi When throwing an Exception, it can either be caught by a catch block in the same immediate scope, thrown up the stack trace to the calling function until caught by a catch block, or finally thrown out of the main function to the operating system to be handled. The last of which cannot be caught and is what you're doing in your code.
8th Aug 2021, 3:24 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Rishi you don't, or can't. If a part of your code might throw exception, you wrap it with try and handle it. If you guarantee that your code doesn't throw exception, you don't wrap it with try.
8th Aug 2021, 2:40 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
Because it is prior to the try block and out of its scope. Move it into the body of the try block and it will be caught.
8th Aug 2021, 2:17 AM
ChaoticDawg
ChaoticDawg - avatar
0
ChaoticDawg yes it works, but how to catch the throw statements which are outside the try block?
8th Aug 2021, 2:22 AM
Rishi
Rishi - avatar