The tag has no wiki summary.

learn more… | top users | synonyms

66
votes
8answers
8k views

Exceptions: Why throw early? Why catch late?

There are many well-known best practices about exception handling in isolation. I know the "do's and don'ts" well enough, but things get complicated when it comes to best practices or patterns in ...
49
votes
7answers
31k views

Why use try … finally without a catch clause?

The classical way to program is with try / catch but when is it appropriate to use try without catch? In Python the following appears legal and can make sense: try: #do work finally: #do ...
42
votes
8answers
8k views

Defensive Programming vs Exception Handling?

I'm working through the book "Head First Python" (it's my language to learn this year) and I got to a section where they argue about two code techniques: Defensive coding vs Exception handling. Here ...
38
votes
7answers
3k views

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 ...
31
votes
5answers
2k views

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 ) ...
30
votes
10answers
10k views

Is catching general exceptions really a bad thing?

I typically agree with most code analysis warnings, and I try to adhere to them. However, I'm having a harder time with this one: CA1031: Do not catch general exception types I understand the ...
27
votes
11answers
10k views

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?
26
votes
11answers
12k views

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 ...
26
votes
3answers
7k views

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 ...
20
votes
4answers
2k views

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 ...
18
votes
5answers
5k views

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. ...
18
votes
2answers
2k views

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 ...
16
votes
10answers
1k views

Why not use the word bug instead of exception? [closed]

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 ...
16
votes
8answers
2k views

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. ...
14
votes
1answer
875 views

Is it fine to make a default constructor unusable?

Specifically asking about the default constructor Given that the constructor initializes all the data for an object, if I create a class that can't be used without proper initialization, is it not ...
14
votes
2answers
1k views

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 ...
13
votes
7answers
1k views

Do exceptions basically exist to prevent a system from crashing?

Second of all, I was wondering if anyone knew what the difference was between exceptions (in the realm of exception control flow) and Exceptions (such as used in Java). But are they there to ...
12
votes
5answers
872 views

Strengthening code with possibly useless exception handling

Is it a good practice to implement useless exception handling, just in case another part of the code is not coded correctly? Basic example A simple one, so I don't loose everybody :). Let's say I'm ...
12
votes
5answers
606 views

Exception handling in a program that needs to run 24/7

I have read that we should only catch exceptions that can be handled, which makes catching the base exception class (C# in this case) a bad idea (on top of other reasons). I am currently part of a ...
11
votes
6answers
705 views

alternatives to nested try-catches for fallbacks

I have a situation where I am trying to retrieve an object. If the lookup fails I have several fallbacks in place, each of which may fail. So the code looks like: try { return ...
10
votes
6answers
2k views

Should you throw an exception if a method's input values are out of range? [duplicate]

Should you throw an exception if a method's input values are out of range? eg //no imaginary numbers public int MySquareRoot(int x) { if (x<0) { throw new ...
10
votes
4answers
439 views

Purposely raising exceptions to use catch

For a typical if...else wrapped with exception handling, is something like the following example a recommended practice to avoid code duplication? try { if (GetDataFromServer()) { ...
9
votes
4answers
646 views

Using “catching exceptions” to improve readability, Good or Bad?

In the section When to Use Exception in The Pragmatic Programmer, the book writes that instead of: retcode = OK; if (socket.read(name) != OK) { retcode = BAD_READ; } else { ...
9
votes
6answers
2k views

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 ...
9
votes
4answers
656 views

Recommend a design pattern/approach to exposing/tolerating/recovering from system errors, Exception handling (e.g.s in Java, C++, Perl, PHP)

Can you recommend a design pattern/approach to exposing/tolerating/recovering from system errors, Exception handling (Java, C++, Perl, PHP)? Some errors need to be reported. Some errors can be ...
9
votes
4answers
1k views

Why design a modern language without an exception-handling mechanism?

Many modern languages provide rich exception handling features, but Apple's Swift programming language does not provide an exception handling mechanism. Steeped in exceptions as I am, I'm having ...
8
votes
8answers
1k views

Using a try-finally (without catch) vs enum-state validation

I have been reading the advice on this question about how an exception should be dealt with as close to where it is raised as possible. My dilemma on the best practice is whether one should use a ...
8
votes
3answers
579 views

Is it a security flaw to log the class and method name when an exception occurs?

I have the following : public class doCheck(){ public void performCheck(){ try { perform all checks...... } catch(Exception e){ ...
8
votes
3answers
7k views

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 ...
8
votes
1answer
1k views

Best exception handling practices or recommendations? [closed]

I think the two main problems with my programs are my code structure/organization and my error handling. I'm reading Code Complete 2, but I need something to read for working with potential problems. ...
8
votes
3answers
1k views

“how bad” is unrelated code in try-catch-finally block?

This is a related Q: Is use of finally clause for doing work after return bad style/dangerous? In the referenced Q, the finally code is related to the structure used and the necessity of ...
8
votes
0answers
1k views

Do uncatchable exceptions exist in Javascript? [migrated]

Do any javascript runtimes (browsers, Node, etc.) ever throw uncatchable exceptions? Are any and all exceptions ever encountered in a javascript environment catchable in a try/catch statement?
7
votes
2answers
1k views

Decision for Unchecked Exceptions in Scala

As a java programmer, I have always been critical of Unchecked Exceptions. Mostly programmers use it as an en-route to coding easiness only to create trouble later. Also the programs (though untidy) ...
7
votes
4answers
416 views

What are good ways of balancing informative exceptions and clean code?

With our public SDK, we tend to want to give very informative messages about why an exception occurs. For example: if (interfaceInstance == null) { string errMsg = string.Format( ...
6
votes
13answers
2k views

Should integer divide by zero halt execution? [closed]

I know that modern languages handle integer divide by zero as an error just like the hardware does, but what if we could design a whole new language? Ignoring existing hardware, what should a ...
6
votes
3answers
2k views

Exception Handling Frequency/Log Detail

I am working on a fairly complex .NET application that interacts with another application. Many single-line statements are possible culprits for throwing an Exception and there is often nothing I can ...
6
votes
4answers
361 views

Logging errors caused by exceptions deep in the application

What are best-practices for logging deep within an application's source? Is it bad practice to have multiple event log entries for a single error? For example, let's say that I have an ETL system ...
6
votes
2answers
428 views

Where should I handle fatal exceptions

Suppose I have a controller that loads a file and hands it over to the processing. Should I handle the exception in the file loader and return Null if something is wrong, or should I throw the ...
5
votes
7answers
2k views

Is it a acceptable approach to put try catch wherever null pointer exception occurs?

There are instances where the references to objects, fields, variables etc can be null and there might be a possible occurrence of Null Pointer exception occurring at that point. Is is a permanent ...
5
votes
2answers
504 views

How should I handle logger failures?

In several of our company's applications, we use a custom logger. It's fairly robust, though we may replace it with something like NLog in the future. One of the logger's tasks is to log any ...
5
votes
3answers
996 views

Authoritative sources for exception handling best practices [closed]

My work place suffers from a bad case of the Pokemon Exception Handling anti-pattern with constructs like the following splattered across the code base: try { ... } catch (Exception ex) { // ...
5
votes
4answers
195 views

What to do when allowing application to run in an invalid state?

We currently have a 'global catch all exceptions' section in our application. When an uncaught exception is thrown, the stack trace is displayed and the application continues running. More often ...
5
votes
2answers
523 views

Should exceptions of a subclass extend the superclass exceptions or my own namespace?

Our library extends another (third-party) library. When we create a child class in our library, and want to throw exceptions, should those exceptions extend the exceptions from the parent class or ...
4
votes
4answers
227 views

Is it OK to let invalid arguments slip to another method?

For example lets take this method: public List<string[]> ReadAll(int listCapacity) { List<string[]> list = new List<string[]>(listCapacity); while (Read()) { ...
4
votes
1answer
462 views

Why would I use Control.Exception in Haskell?

I'm trying to really master Haskell error handling, and I've gotten to the point where I don't understand why I would use Control.Exception instead of Control.Monad.Error. The way I can see it, I can ...
3
votes
4answers
408 views

Java - Is it possible/good idea to reduce chance of crashing by catching Error?

I have a class the implements A which will run a certain method of class B. There is a requirement that this A should never crash when running this operation (which is not possible, right?). To ...
3
votes
4answers
206 views

How much data should exceptions hold?

Almost all the exceptions I have ever written have been very lightweight, containing a String message and optionally a throwable. In some situations I have included some application specific enum or ...
3
votes
4answers
284 views

Using configuration to determine whether to handle exception or bubble it up

On a side project I'm working on I came up with a way of handling exceptions that's adjustable by configuration. So a try/catch block might look like this: try { fileHelper.MoveFile(file, ...
3
votes
3answers
134 views

Using Exception Emails as a System Status Indicator

I'm working through our bug list today and I'm trying to clear up/fix issues that we commonly receive exception emails for. Although all the exceptions (so far) are handled, some don't actually ...
3
votes
3answers
319 views

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 ...