Questions tagged [rethrow]

use for issues on rethrowing catch exception

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

73 questions
5
votes
1 answer

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 declaring two seperate functions, with the same…
Alexander
  • 59,041
  • 12
  • 98
  • 151
5
votes
2 answers

Will C++ throw with no arguments work inside another frame to rethrow an exception?

If I have a code like the following: try { doSomething(); } catch (...) { noteError(); } void noteError() { try { throw; } catch (std::exception &err) { std::cerr << "Note known error here: " << err.what(); } catch (...) { …
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
4
votes
1 answer

Why do "throw" and "throw ex" have the same behavior in this case?

I'm flabbergasted. I always thought that throw by itself in a catch block would throw the exception at hand without altering the stack trace, but that throw ex in a catch block would alter the stack trace to show the exception originating at the…
Stephan G
  • 3,289
  • 4
  • 30
  • 49
4
votes
1 answer

Java - More Precise Rethrown Feature

on oracle oficial site write:(http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html#rethrow) In detail, in Java SE 7 and later, when you declare one or more exception types in a catch clause, and rethrow the exception…
4
votes
1 answer

Throw inside Catch in C#

Found something yesterday that made me realise that I'm most likely still missing fundamental tidbits about C#. I have a Stateless Service Fabric Application. I have a try-catch around the main while-loop. If I throw an exception in this loop, it…
Kagemand Andersen
  • 1,470
  • 2
  • 16
  • 30
4
votes
3 answers

How to detect a bad way of re-throwing a C# Exception using StyleCop or VS2010?

My colleagues are seasoned C++ hackers switching to .Net. One of the mistakes that they make unintentionally is writing code like this: catch(ArgumentExcepttion ae) { // Code here logs the exception message // And this is supposed to…
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
3
votes
4 answers

An Exception Handling Class

What is the best practice for handling exceptions without having to put try/catch blocks everywhere? I had the idea of creating a class that is devoted to receiving and handling exceptions, but I am wondering if its a good design idea. Such a class…
Sean Thoman
  • 7,429
  • 6
  • 56
  • 103
3
votes
2 answers

C++: object slicing and exceptions

In an interview I was asked why catching exceptions by value can be a problem and I answered that this can cause object slicing. And this is what I find in the Internet, for example here: https://www.viva64.com/en/w/v746/ But now I am trying to…
Andrey Rubliov
  • 1,359
  • 2
  • 17
  • 24
3
votes
1 answer

Should I rethrow an exception in this case?

Is this approach ok? Am I handling exceptions correctly? See my class: class Email extends String { protected function validate($email) { try{ parent::validate($email); } catch(InvalidArgumentException $e) { throw $e; } …
fabio
  • 2,269
  • 5
  • 22
  • 34
3
votes
1 answer

Exception thrown in finally and catch block

have question on exception thrown in catch and finally block: class MyExc1 extends Exception {} class MyExc2 extends Exception {} class MyExc3 extends MyExc2 {} public class C1 { public static void main(String[] args) throws Exception { …
3
votes
2 answers

Rethrowing an exception in C#

I have some code which catches the exception, rolls back the transaction and then rethrow the exception. catch ( Exception exSys ) { bqBusinessQuery.RollBackTransaction(); throw exSys ; } If I use this code, VS Code analysis throws…
jitendragarg
  • 945
  • 1
  • 14
  • 54
3
votes
2 answers

Why does ReSharper recommend the removal of its own fix?

ReSharper recommends rethrowing an exception and then, when I do that, it says the entire catch clause is redundant anyway, and suggests it be removed. I am using this code (from MethodMan here): public static DataTable ExecuteDataSet(string sql,…
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
2
votes
0 answers

Any downsides to rethrowing the *cause* of a checked exception as an unchecked exception?

I am writing a library in which I use checked exceptions internally as it helps to make sure that all paths handle certain exceptional cases. However, I don't want to burden the users of my library with these checked exceptions because of this rule…
john16384
  • 7,800
  • 2
  • 30
  • 44
2
votes
0 answers

Groovy catch a parent Exception but NOT a children Exception

(context: integrating with an application -Jenkins- with various plugins not all with good design) I have these exception: public class AbortException extends IOException and in some cases I want to catch IOException but avoid catching the…
2
votes
1 answer

std::rethrow_exception(nullptr) undefined behavior or bad_exception?

I have a code snippet where I call rethrow_exception with nullptr as argument. The documentation says the argument should be non-null, but I want to know, if I pass nullptr, is the behavior undefined or known? I am getting bad_exception everytime.…
Ishita
  • 489
  • 5
  • 15