nested-exceptions

Best way to check whether a certain exception type was the cause (of a cause, etc …) in a nested exception?

浪子不回头ぞ 提交于 2019-12-20 10:34:18
问题 I am writing some JUnit tests that verify that an exception of type MyCustomException is thrown. However, this exception is wrapped in other exceptions a number of times, e.g. in an InvocationTargetException, which in turn is wrapped in a RuntimeException. What's the best way to determine whether MyCustomException somehow caused the exception that I actually catch? I would like to do something like this (see underlined): try { doSomethingPotentiallyExceptional(); fail("Expected an exception."

How does this implementation of chaining exceptions work?

前提是你 提交于 2019-12-06 08:23:52
问题 I previously asked a question about how to chaining exceptions in C++, and one of the answers provided a nifty solution to how it can be done. The problem is that I don't understand the code, and trying to have this kind of discussion in the comments is just too much of a bother. So I figured it's better to start a new question entirely. The code is included below and I've clearly marked each section which I don't get. A description of what I don't understand is included below the code. The

Best way to check whether a certain exception type was the cause (of a cause, etc …) in a nested exception?

守給你的承諾、 提交于 2019-12-02 22:06:37
I am writing some JUnit tests that verify that an exception of type MyCustomException is thrown. However, this exception is wrapped in other exceptions a number of times, e.g. in an InvocationTargetException, which in turn is wrapped in a RuntimeException. What's the best way to determine whether MyCustomException somehow caused the exception that I actually catch? I would like to do something like this (see underlined): try { doSomethingPotentiallyExceptional(); fail("Expected an exception."); } catch (RuntimeException e) { if (!e. wasCausedBy (MyCustomException.class) fail("Expected a

Should exceptions be chained in C++? [duplicate]

删除回忆录丶 提交于 2019-11-30 08:56:42
This question already has an answer here: Proper/elegant way of implementing C++ exception chaining? 4 answers How to simulate inner exception in C++ 6 answers I just finished work on a C++-program where I've implemented my own exceptions (although derived from std::exception). The practice I've applied when one exception causes a chain reaction, propagating the error upwards and giving rise to other exceptions, is to concatenate the error message at each appropriate step across the modules (read classes). I.e. the old exception itself is dropped and a new exception is created, but with a

Should exceptions be chained in C++? [duplicate]

空扰寡人 提交于 2019-11-29 13:10:39
问题 This question already has an answer here: Proper/elegant way of implementing C++ exception chaining? 4 answers How to simulate inner exception in C++ 6 answers I just finished work on a C++-program where I've implemented my own exceptions (although derived from std::exception). The practice I've applied when one exception causes a chain reaction, propagating the error upwards and giving rise to other exceptions, is to concatenate the error message at each appropriate step across the modules

Catching an exception that is nested into another exception

时光毁灭记忆、已成空白 提交于 2019-11-27 21:54:58
I want to catch an exception, that is nested into another exception. I'm doing it currently this way: } catch (RemoteAccessException e) { if (e != null && e.getCause() != null && e.getCause().getCause() != null) { MyException etrp = (MyException) e.getCause().getCause(); ... } else { throw new IllegalStateException("Error at calling service 'service'"); } } Is there a way to do this more efficient and elegant? There is no more elegant way of selectively "catching" nested exceptions. I suppose if you did this kind of nested exception catching a lot, you could possibly refactor the code into a

Why doesn't C++ use std::nested_exception to allow throwing from destructor?

爷,独闯天下 提交于 2019-11-27 01:37:46
The main problem with throwing exceptions from destructor is that in the moment when destructor is called another exception may be "in flight" ( std::uncaught_exception() == true ) and so it is not obvious what to do in that case. "Overwriting" the old exception with the new one would be the one of the possible ways to handle this situation. But it was decided that std::terminate (or another std::terminate_handler ) must be called in such cases. C++11 introduced nested exceptions feature via std::nested_exception class. This feature could be used to solve the problem described above. The old

Catching an exception that is nested into another exception

岁酱吖の 提交于 2019-11-26 23:06:47
问题 I want to catch an exception, that is nested into another exception. I'm doing it currently this way: } catch (RemoteAccessException e) { if (e != null && e.getCause() != null && e.getCause().getCause() != null) { MyException etrp = (MyException) e.getCause().getCause(); ... } else { throw new IllegalStateException("Error at calling service 'service'"); } } Is there a way to do this more efficient and elegant? 回答1: There is no more elegant way of selectively "catching" nested exceptions. I

Why doesn't C++ use std::nested_exception to allow throwing from destructor?

时光怂恿深爱的人放手 提交于 2019-11-26 09:47:39
问题 The main problem with throwing exceptions from destructor is that in the moment when destructor is called another exception may be \"in flight\" ( std::uncaught_exception() == true ) and so it is not obvious what to do in that case. \"Overwriting\" the old exception with the new one would be the one of the possible ways to handle this situation. But it was decided that std::terminate (or another std::terminate_handler ) must be called in such cases. C++11 introduced nested exceptions feature