Questions tagged [rethrow]

use for issues on rethrowing catch exception

In terms of exception handling, this refers to throwing the exception again.

73 questions
307
votes
11 answers

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 difference between the following code blocks in the way…
Seibar
  • 68,705
  • 38
  • 88
  • 99
137
votes
5 answers

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 here"); throw; // Does the rethrow exception reflect…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
103
votes
2 answers

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 confusing when try to understand how we should use…
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
48
votes
3 answers

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: do_something_dangerous() except: …
Bosiwow
  • 2,025
  • 3
  • 28
  • 46
43
votes
12 answers

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 } catch (Exception ex) { throw; //Line…
Floyd
  • 1,898
  • 12
  • 20
27
votes
3 answers

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 since it is not inside the try block and I see in another…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
20
votes
6 answers

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 { File.Open("FileNotFound.txt",…
orandov
  • 3,256
  • 3
  • 32
  • 32
15
votes
4 answers

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(Thread thread, Throwable throwable) { …
Eternal Learner
  • 2,602
  • 5
  • 27
  • 38
10
votes
5 answers

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 :…
Pragmateek
  • 13,174
  • 9
  • 74
  • 108
10
votes
2 answers

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 ExceptionA, ExceptionB or others } catch…
gsf
  • 6,612
  • 7
  • 35
  • 64
9
votes
3 answers

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(); o.execute(); …
Shahar
  • 478
  • 5
  • 17
7
votes
1 answer

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's running on a Windows 10 machine; both Visual…
Philipp B.
  • 81
  • 4
7
votes
6 answers

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 above example, on the bold line, I am getting PMD…
user613114
  • 2,731
  • 11
  • 47
  • 73
6
votes
2 answers

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 both FirstException and SecondException } …
Julien
  • 1,278
  • 12
  • 26
5
votes
3 answers

When to use multi-catch and when to use rethrow?

I'm very uncertain about this two topics. I know that i should use multi-catch for exceptions which need to handle the same way. But for what purpose do i really need something like that. private void something(String name) throws IOException,…
LaBlum
  • 61
  • 5
1
2 3 4 5