73 questions
311
votes
11
answers
197k
views
Best practices for catching and re-throwing .NET exceptions
What are the best practices to consider when catching exceptions and re-throwing them? I want to make sure that the Exception object's InnerException and stack trace are preserved. Is there a ...
144
votes
5
answers
94k
views
C++ Exceptions questions on rethrow of original exception
Will the following append() in the catch cause the rethrown exception to see the effect of append() being called?
try {
mayThrowMyErr();
} catch (myErr &err) {
err.append("Add to my message ...
113
votes
2
answers
27k
views
What are the differences between throws and rethrows in Swift?
After searching for some references to figure it out, -unfortunately- I could not find useful -and simple- description about understanding the differences between throws and rethrows. It is kind of ...
67
votes
3
answers
104k
views
rethrowing python exception. Which to catch?
I'm learning to use python. I just came across this article:
http://nedbatchelder.com/blog/200711/rethrowing_exceptions_in_python.html
It describes rethrowing exceptions in python, like this:
try:
...
43
votes
12
answers
8k
views
Incorrect stacktrace by rethrow
I rethrow an exception with "throw;", but the stacktrace is incorrect:
static void Main(string[] args) {
try {
try {
throw new Exception("Test"); //Line 12
}
...
27
votes
3
answers
12k
views
Will exception thrown in catch block be caught by later catch blocks?
Consider the following C++ code:
try {
throw foo(1);
} catch (foo &err) {
throw bar(2);
} catch (bar &err) {
// Will throw of bar(2) be caught here?
}
I would expect the answer is no ...
20
votes
6
answers
7k
views
Why does resharper say 'Catch clause with single 'throw' statement is redundant'?
I thought throwing an exception is good practice to let it bubble back up to the UI or somewhere where you log the exception and notify the user about it.
Why does resharper say it is redundant?
try
...
16
votes
4
answers
6k
views
How to re-throw an exception
In my onCreate() I set an UncaughtException handler as follows:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(...
10
votes
5
answers
4k
views
Throw VS rethrow : same result?
refering to a lot of documentation on the net, particularly on SO, eg : What is the proper way to re-throw an exception in C#?
there should be a difference between "throw e;" and "throw;".
But, from :...
10
votes
2
answers
14k
views
rethrow java exception with new message, preserving the exception type if it is in the method declaration list
I am trying to create a helper method that will eliminate the need of having code like this:
void foo() throws ExceptionA, ExceptionB, DefaultException {
try {
doSomething(); // that throws ...
9
votes
3
answers
7k
views
Immediately rethrowing in catch block and using finally
I have a wrapper responsible for logging operations, named OperationWrapper. Its structure is simple and as follows:
public void runOperation(Operation o) throws Exception{
logOperationStarted()...
7
votes
6
answers
22k
views
Java error: New exception is thrown in catch block, original stack trace may be lost
try {
// code which throws exception.
} catch (SQLException sqlex) {
logger.error("Custom message", sqlex);
**throw new CustomApplicationException("Custom message", sqlex);**
}
In the ...
6
votes
1
answer
465
views
Save rethrowing function as a non-throwing closure
As far as I understand, rethrows essentially creates two functions from a single declaration/definition, like so:
func f(_ c: () throws -> Void) rethrows { try c()}
// has the same effect as ...
6
votes
1
answer
236
views
Is there a memory leak in my machine's implementation of std::exception_ptr, std::current_exception and rethrow_exception?
When running the following code, I see the memory consumption of the process grow. Is there a memory leak in my code, is there a memory leak in the std implementation, or is it intended behaviour? It'...
6
votes
2
answers
387
views
Java 7 precise rethrow and legacy code
The more precise rethrow allows to write code that throws the exception really thrown :
public void foo(String bar) throws FirstException, SecondException {
try{
// Code that may throw ...