Questions tagged [exception-handling]

Exception handling is the process of responding to the occurrence of anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution.

In general, an exception is handled (resolved) by saving the current state of execution in a predefined place and switching the execution to a specific subroutine known as an exception handler. If exceptions are continuable, the handler may later resume the execution at the original location using the saved information.

From here

139 questions
150
votes
13 answers

Is using nested try-catch blocks an anti-pattern?

Is this an antipattern? It is an acceptable practice? try { //do something } catch (Exception e) { try { //do something in the same line, but being less ambitious } catch (Exception ex) { try…
Mister Smith
  • 2,867
  • 4
  • 21
  • 17
84
votes
13 answers

Is it good practice to catch a checked exception and throw a RuntimeException?

I read some code of a colleague and found that he often catches various exceptions and then always throws a 'RuntimeException' instead. I always thought this is very bad practice. Am I wrong?
RoflcoptrException
  • 2,035
  • 2
  • 17
  • 18
53
votes
3 answers

Why are exception specifications bad?

Back in school some 10+ years ago, they were teaching you to use exception specifiers. Since my background is as one of them Torvaldish C programmers who stubbornly avoids C++ unless forced to, I only end up in C++ sporadically, and when I do I…
user29079
50
votes
7 answers

Workaround for Java checked exceptions

I appreciate a lot the new Java 8 features about lambdas and default methods interfaces. Yet, I still get bored with checked exceptions. For instance, if I just want to list all the visible fields of an object I would like to simply write this: …
50
votes
5 answers

should I throw exception from constructor?

I know I can throw exception from constructor in PHP but should I do it? For example, if a parameter's value is not as I expected it. Or should I defer throwing an exception till a method is invoked. What are advantages and disadvantages in both…
WordsWorth
  • 621
  • 1
  • 6
  • 9
47
votes
8 answers

Are there legitimate reasons for returning exception objects instead of throwing them?

This question is intended to apply to any OO programming language that supports exception handling; I am using C# for illustrative purposes only. Exceptions are usually intended to be raised when an problem arises that the code cannot immediately…
stakx
  • 2,138
  • 18
  • 29
40
votes
7 answers

Maybe monad vs exceptions

I wonder what are the advantages of Maybe monad over exceptions? It looks like Maybe is just explicit (and rather space-consuming) way of try..catch syntax. update Please note that I'm intentionally not mentioning Haskell.
39
votes
4 answers

Why are brackets required for try-catch?

In various languages (Java at least, think also C#?) you can do things like if( condition ) singleStatement; while( condition ) singleStatement; for( var; condition; increment ) singleStatement; So when I have just one statement, I…
Svish
  • 1,092
  • 8
  • 14
35
votes
5 answers

How to deal with checked exceptions that cannot ever be thrown

Example: foobar = new InputStreamReader(p.getInputStream(), "ISO-8859-1"); Since the encoding is hardcoded and correct, the constructor will never throw the UnsupportedEncodingException declared in the specification (unless the java implementation…
user281377
  • 28,352
  • 5
  • 75
  • 130
33
votes
4 answers

Throwing an exception inside finally

Static code analyzers like Fortify "complain" when an exception might be thrown inside a finally block, saying that Using a throw statement inside a finally block breaks the logical progression through the try-catch-finally. Normally I agree with…
superM
  • 7,363
  • 4
  • 29
  • 38
31
votes
3 answers

Should service layer catch all dao exceptions and wrap them as service exceptions?

I have three layer Spring web app: dao, service and controllers. A controller never calls directly the dao, it does it through the service layer. Right now, most of the time if there is dao exception (runtime) that is not handled, it'll be caught…
Oscar
  • 453
  • 1
  • 4
  • 7
28
votes
12 answers

Is the 'finally' portion of a 'try ... catch ... finally' construct even necessary?

Some languages (such as C++ and early versions of PHP) don't support the finally part of a try ... catch ... finally construct. Is finally ever necessary? Because the code in it always runs, why wouldn't/shouldn't I just place that code after a try…
Agi Hammerthief
  • 564
  • 1
  • 5
  • 16
26
votes
10 answers

Error handling - Should a program fail on errors or silently ignore them

I'm writing a simple little program to transmit MIDI over a network. I know that the program will encounter transmission problems and / or other exception situations that I won't be able to predict. For the exception handling, I see two approaches. …
Arlen Beiler
  • 413
  • 1
  • 4
  • 9
20
votes
2 answers

Abstract exception super type

If throwing System.Exception is considered so bad, why wasn't Exception made abstract in the first place? That way, it would not be possible to call: throw new Exception("Error occurred."); This would enforce using derived exceptions to provide…
marco-fiset
  • 8,721
  • 9
  • 35
  • 46
19
votes
10 answers

Why not use the word bug instead of exception?

If we refer to exceptions as bugs, why not just call it a bug in the first place instead of an exception? If in the code it's called exception and as soon as it occurs it's called a bug. Then why not call it a bug in the first place? Thank you for…
1
2 3
9 10