因此,我尝试基于 Java Exception 类为 C++ 编写一个简单的 Exception 基类。 我确信那里已经有很棒的库,但我这样做是为了练习,而不是生产代码,我很好奇并且一直在寻求学习。 Java Exception 所做的其中一件事是“原因”的概念,我也想实现这一点。在 Java 中,带有原因的新异常如下所示:
Exception cause = new Exception();
Exception newExcept = new Exception(cause);
但是,在 C++ 中,将异常作为参数传递给构造函数是调用复制构造函数的方式。因此,在复制异常和创建具有原因的新异常之间存在概念上的脱节。显然,这在 Java 中不是问题。
我想我只是想知道处理此问题的最佳方法是什么。我的一些想法是:
Exception(Exception &) 和 cause 的构造函数是 Exception(Exception *)谢谢
最佳答案
异常 - 当在堆栈上分配时(我强烈推荐这个) - 在 catch 子句之后被释放。所以你需要在新创建的异常中创建一个“内部”异常的拷贝。如果您捕获异常的基类,它将丢失它的正确类型,除非您为异常提供克隆方法。
#include <string>
#include <exception>
class MyBaseException : public std::exception
{
public:
MyBaseException(const std::string& what = std::string("MyBaseException"))
: m_BaseException(0), m_What(what) {} //Constructor without inner exception
MyBaseException(const MyBaseException& innerException, const std::string& what = std::string("MyBaseException"))
: m_BaseException(innerException.clone()), m_What(what) {} //Constructor with inner exception
template <class T> // valid for all subclasses of std::exception
MyBaseException(const T& innerException, const std::string& what = std::string("MyBaseException"))
: m_BaseException(new T(innerException)), m_What(what) {}
virtual ~MyBaseException() throw()
{ if(m_BaseException) { delete m_BaseException; } } //don't forget to free the copy of the inner exception
const std::exception* base_exception() { return m_BaseException; }
virtual const char* what() const throw()
{ return m_What.c_str(); } //add formated output for your inner exception here
private:
const std::exception* m_BaseException;
const std::string m_What;
virtual const std::exception* clone() const
{ return new MyBaseException(); } // do what ever is necesary to copy yourselve
};
int main(int argc, char *argv[])
{
try {
try {
throw std::exception();
}
catch(const std::exception& e) {
throw MyBaseException(e, "bad");
}
}
catch (const MyBaseException& e) {
throw MyBaseException(e, "even worse");
}
//throw MyBaseException(1, "will not compile");
}
关于java - 在 C++ 中实现异常类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3682294/
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在学习Rails,并阅读了关于乐观锁的内容。我已将类型为integer的lock_version列添加到我的articles表中。但现在每当我第一次尝试更新记录时,我都会收到StaleObjectError异常。这是我的迁移:classAddLockVersionToArticle当我尝试通过Rails控制台更新文章时:article=Article.first=>#我这样做:article.title="newtitle"article.save我明白了:(0.3ms)begintransaction(0.3ms)UPDATE"articles"SET"title"='dwdwd
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我需要在RubyonRails中实现无向图G=(V,E)并考虑构建一个Vertex和一个Edge模型,其中Vertex有_多条边。由于边恰好连接两个顶点,您将如何在Rails中执行此操作?您是否知道任何有助于实现此类图表的gem或库(对重新发明轮子不感兴趣;-))? 最佳答案 不知道有任何现有库在ActiveRecord之上提供图形逻辑。您可能必须实现自己的Vertex、EdgeActiveRecord支持的模型(请参阅Rails安装的rails/activerecord中的vertex.rb和edge.rb/test/fixtur
我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。问题1)我想知道rubyonrails是否有功能类似于primefaces的gem。我问的原因是如果您使用primefaces(http://www.primefaces.org/showcase-labs/ui/home.jsf),开发人员无需担心javascript或jquery的东西。据我所知,JSF是一个规范,基于规范的各种可用实现,prim