Questions tagged [catch-block]

The `catch-block` in a program is used to handle an `exception` thrown by a block of code.

144 questions
5
votes
5 answers

try-catch block in Java - execution statements in catch code

I have a question about the order of the execution of statements in a catch block in Java. when I run the following class Test1 (see below), I expect to have as output first Hi!, then the result of the e.printStackTrace(); statement, and then Bye!.…
user42155
  • 48,965
  • 27
  • 59
  • 60
5
votes
3 answers

Catching Multiple Exceptions - calling methods not present in Exception on the caught exception

public class try { public static void main(String[] args) { try { if(true) throw new A(); if(true) throw new B(); } catch( A | B e) { …
user93353
  • 13,733
  • 8
  • 60
  • 122
5
votes
3 answers

Questions regarding ordering of catch statements in catch block - compiler specific or language standard?

I am currently using Visual Studio Express C++ 2008, and have some questions about catch block ordering. Unfortunately, I could not find the answer on the internet so I am posing these questions to the experts. I notice that unless catch (...) is…
Andy
  • 2,770
  • 9
  • 35
  • 42
5
votes
6 answers

Return statements in catch blocks

I have seen some developers use the return statement in a catch block. Why/when would this be a useful technique to employ? EDIT: I actually just saw the return keyword being used.
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
5
votes
4 answers

Exceptions to Never Catch

I know that there are some exception types that cannot be caught in catch blocks, like StackOverflowException in .NET 2.0. I would like to know which other exceptions are inadvisable to catch, or are associated with bad practices. The way I would…
CShearholdt
  • 53
  • 1
  • 5
4
votes
2 answers

Extending catch prototype for new relic tracking

I want to extend the general catch prototype of the Promise object, so that I can automatically log the error to out application monitoring whenever a catch block is hit. But I have trouble getting the error object out of the Promise object when…
Sebsemillia
  • 9,366
  • 2
  • 55
  • 70
4
votes
7 answers

Catching exceptions in Java

There are certain predefined exceptions in Java, which, if thrown, report that something serious has happened and you'd better improve your code, than catching them in a catch block (if I have understood it correctly). But still I find many programs…
user42155
  • 48,965
  • 27
  • 59
  • 60
4
votes
2 answers

Will throwing an exception in a catch block lead to two exceptions being in flight?

Consider the following C++ code: class MyException {}; void someFunction() { try { /// ... code that may throw } catch(std::exception& e ) { throw MyException(); } } Question Is the exception e absorbed at…
Knitschi
  • 2,822
  • 3
  • 32
  • 51
4
votes
4 answers

Java: Can catch blocks be polymorphic?

In a paper I'm going over for a repeat exam, I'm asked "Can catch blocks be polymorphic?". If true, it doesn't make sense to me to call multiple catch blocks polymorphic. Is it polymorphism if catch blocks cannot be named and only contain parameters…
DagdA
  • 484
  • 1
  • 7
  • 25
3
votes
3 answers

How to set up a default operation whenever a catch block get executed?

For debugging purposes i want my Java application to upload a log file on my MySql database whenever a catch clause is executed, regardless of which exception is thrown. The worst solution i thought to was to add my uploader method into each catch…
Zartof
  • 185
  • 1
  • 12
3
votes
2 answers

Java - How to Test Catch Block?

Bit of a repost, but a certain catch-22 about not having enough reputation means I can't comment on any of the duplicate threads! (cough cough) I'm trying to test the catch block of a try-catch using Mockito; is it possible to make a mock throw an…
Danny Salvadori
  • 51
  • 1
  • 1
  • 4
3
votes
1 answer

first catch catches any type

this bit of code is driving me nuts: #include #include int main() { std::string test = "foo"; try { throw test; } catch (const int &x) { std::cout << "int " << x << "\n"; } catch…
usePtr
  • 31
  • 2
3
votes
1 answer

Try/Catch — How do I know what to catch in odd/complex cases?

I know why I shouldn't use open catch blocks like so: int x = 0; try { x = GetXFromSomeplaceThatCanFail(); } catch //Possibly (Exception) or (Exception e) { //Ignore The Failure Because We Don't Care If It Fails } if (x != 0) //Yes I know I…
Yushatak
  • 741
  • 1
  • 5
  • 15
3
votes
2 answers

why ... (three points) in catch block is exist?

In the try catch statement we can do: try{} catch(...){} As far as I know, ... means any exception. My question is: Why the C++ standard chose this way (...) instead of just ()? while, for example, in functions if you do not need parameters you…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
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
1
2
3
9 10