Exceptions
Multiple Catches
Different exception types can be handled by different catch blocks.
multiple catches
Catch blocks are tested in order, so specific exception types should appear before broader handlers.
Multiple Catches
multiple_catches.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <stdexcept>
#include <string>
int loadScore(const std::string& mode) {
if (mode == "missing") {
throw std::invalid_argument("missing");
}
if (mode == "large") {
throw std::out_of_range("large");
}
return 88;
}
int main() {
std::string mode = "ok";
try {
int score = loadScore(mode);
std::cout << "score=" << score << std::endl;
} catch (const std::invalid_argument& error) {
std::cout << "problem=missing" << std::endl;
} catch (const std::out_of_range& error) {
std::cout << "problem=range" << std::endl;
}
std::cout << "mode=" << mode << std::endl;
return 0;
}
#include <iostream>
#include <stdexcept>
#include <string>
int loadScore(const std::string& mode) {
if (mode == "missing") {
throw std::invalid_argument("missing");
}
if (mode == "large") {
throw std::out_of_range("large");
}
return 88;
}
int main() {
std::string mode = "missing";
try {
int score = loadScore(mode);
std::cout << "score=" << score << std::endl;
} catch (const std::invalid_argument& error) {
std::cout << "problem=missing" << std::endl;
} catch (const std::out_of_range& error) {
std::cout << "problem=range" << std::endl;
}
std::cout << "mode=" << mode << std::endl;
return 0;
}
#include <iostream>
#include <stdexcept>
#include <string>
int loadScore(const std::string& mode) {
if (mode == "missing") {
throw std::invalid_argument("missing");
}
if (mode == "large") {
throw std::out_of_range("large");
}
return 88;
}
int main() {
std::string mode = "large";
try {
int score = loadScore(mode);
std::cout << "score=" << score << std::endl;
} catch (const std::invalid_argument& error) {
std::cout << "problem=missing" << std::endl;
} catch (const std::out_of_range& error) {
std::cout << "problem=range" << std::endl;
}
std::cout << "mode=" << mode << std::endl;
return 0;
}
mode ← ok
15int main() {16 std::string mode→ ok = "ok"; //@mode="missing", "large"try
18try {19 int score = loadScore(modeok);20 std::cout << "score=" << score << std::endl;int loadScore(const std::string& mode)
5int loadScore(const std::string& modeok) {6 if (mode == "missing") {7 throw std::invalid_argument("missing");8 }9 if (mode == "large") {10 throw std::out_of_range("large");11 }12 return 88;13}score ← 88
18try {19 int score→ 88 = loadScore(modeok);20 std::cout << "score=" << score88 << std::endl;21} catch (const std::invalid_argument& error) {outputscore=88std::cout << "mode=" << mode << std::endl;
27 std::cout << "mode=" << modeok << std::endl;28 return 0;29}outputmode=ok
mode ← missing
15int main() {16 std::string mode→ missing = "missing";try
18try {19 int score = loadScore(modemissing);20 std::cout << "score=" << score << std::endl;int loadScore(const std::string& mode)
5int loadScore(const std::string& modemissing) {6 if (mode == "missing") {if (mode == "missing")
5int loadScore(const std::string& mode) {6 if (modemissing == "missing") {7 throw std::invalid_argument("missing");8 }catch (const std::invalid_argument& error)
20 std::cout << "score=" << score << std::endl;21} catch (const std::invalid_argument& error) {22 std::cout << "problem=missing" << std::endl;23} catch (const std::out_of_range& error) {outputproblem=missingstd::cout << "mode=" << mode << std::endl;
27 std::cout << "mode=" << modemissing << std::endl;28 return 0;29}outputmode=missing
mode ← large
15int main() {16 std::string mode→ large = "large";try
18try {19 int score = loadScore(modelarge);20 std::cout << "score=" << score << std::endl;int loadScore(const std::string& mode)
5int loadScore(const std::string& modelarge) {6 if (mode == "missing") {if (mode == "large")
8}9if (modelarge == "large") {10 throw std::out_of_range("large");11}catch (const std::out_of_range& error)
22 std::cout << "problem=missing" << std::endl;23} catch (const std::out_of_range& error) {24 std::cout << "problem=range" << std::endl;25}outputproblem=rangestd::cout << "mode=" << mode << std::endl;
27 std::cout << "mode=" << modelarge << std::endl;28 return 0;29}outputmode=large