Can a try catch block have multiple throw statements? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can a try catch block have multiple throw statements?

hey folks, I'm a little confused about the exception handling. Can a try/catch block only contain one throw statement? Can the catch statement handle more than one throws?? I'm wondering why the lesson only used one throw statement and not show how to handle multiple throws..? So is it possible?

24th Apr 2020, 10:41 PM
Donovan Thompson
Donovan Thompson - avatar
1 Answer
+ 1
It can contain multiple throw Statements... And, for sure, you can catch multiple of it.. try{ throw 567; //throw std::range_error("Message"); } catch(int e){ ... } catch(const std::range_error& e){ ... } catch(...){ ... } This is the way to catch different exceptions in the order written... catch(...) will catch all exceptions, but should be used very carefully... Hope this helps :)
24th Apr 2020, 11:30 PM
G B
G B - avatar