c++ - Different behavior when passing exception by value vs by reference C++
问题描述
So I understand the basic concept of passing by value or reference. In this example I'm trying to catch an exception either by value or by reference:
#include <iostream>
int main() {
try {
throw std::invalid_argument("nope");
} catch(std::exception &e) {
std::cout << e.what() << std::endl;
}
}
Outputs: "nope"
#include <iostream>
int main() {
try {
throw std::invalid_argument("nope");
} catch(std::exception e) {
std::cout << e.what() << std::endl;
}
}
(notice the missing &) Outputs: "std::exception"
Shouldn't I get the same output given that the second version "copies" e?
解决方案
推荐阅读
- azure-cosmosdb - 如果查询 RU 成本超过限制,如何从 Microsoft Azure Cosmos DB 中删除所有边
- java - Youtube播放器自定义应用强制每次关闭
- c# - ASP Net Core 中使用 SqlConnection 的存储过程执行中的随机请求减慢
- python - 使用现有变量向数据框添加新列
- javascript - 动画风格缓动、缓动等在我的 javascript 代码中不起作用
- javascript - 在 R Shiny 中的旋钮输入显示后添加百分比符号的问题
- php - 下拉值在第一个空格后被截断
- dart - Fluter - Navigator 不返回(黑屏)
- node.js - 如何将节点 gd 图像转换为我可以通过管道传输的流?
- python - 基于自定义字母顺序python对数组进行排序