An exception is an occurrence in an application process that requires deviation from the program's normal flow.

learn more… | top users | synonyms

18
votes
4answers
929 views

Using assertions versus throwing exceptions?

Often when I write a functions I want to make sure the inputs to it are valid in order to detect such errors as early as possible (I believe these are called preconditions). When a precondition fails, ...
15
votes
6answers
4k views

Efficient try / catch block usage?

Should catch blocks be used for writing logic i.e. handle flow control etc? Or just for throwing exceptions? Does it effect efficiency or maintainability of code? What are the side effects (if there ...
75
votes
20answers
10k views

Are null references really a bad thing?

I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake". But why? Sure, they can cause NullReferenceExceptions, but so what? Any element of ...
22
votes
9answers
9k 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?
13
votes
5answers
4k views

Are exceptions as control flow considered a serious antipattern? If so, Why?

Back in the late 90's I worked quite a bit with a code base that used exceptions as flow control. It implemented a finite state machine to drive telephony applications. Lately I am reminded of those ...
8
votes
4answers
966 views

Is rethrowing an exception leaking an abstraction?

I have an interface method that states in documentation it will throw a specific type of exception. An implementation of that method uses something that throws an exception. The internal exception ...
53
votes
11answers
4k views

I've been told that Exceptions should only be used in exceptional cases. How do I know if my case is exceptional?

My specific case here is that the user can pass in a string into the application, the application parses it and assigns it to structured objects. Sometimes the user may type in something invalid. ...
41
votes
7answers
26k 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 ...
11
votes
9answers
4k views

if/else statements or exceptions [duplicate]

Possible Duplicate: Defensive Programming vs Exception Handling? I don't know, that this question fit better on this site, or Stack Overflow, but because my question is connected rather to ...
35
votes
11answers
4k views

Are error variables an anti-pattern or good design?

In order to handle several possible errors that shouldn't halt execution, I have an error variable that clients can check and use to throw exceptions. Is this an Anti-Pattern? Is there a better way to ...
36
votes
20answers
5k views

Is it ever ok to have an empty catch statement?

I thought about it and could not come up with an example. Why would somebody want to catch an exception and do nothing about it? Can you give an example? Maybe it is just something that should never ...
9
votes
2answers
317 views

Use an else after exception (or not)

Consider this bit of code: if (x == 1) { throw "no good; aborting" ; } [... more code ...] Now consider this code: if (x == 1) { throw "no good; aborting" ; } else { [... more code ...] } ...
7
votes
6answers
2k views

Is throwing an exception from a property bad form?

I've always been of the mindset that properties (ie, their set/get operations) should be fast/immediate and failure-free. You should never have to try/catch around getting or setting a property. But ...
3
votes
5answers
663 views

How should I handle exception that *should* never be thrown? [duplicate]

What is the best way to handle errors that shouldn't ever happen? My current way to do this is to throw an exception if the 'thing that shouldn't happen' does happen, like so: /* * Restoring from a ...
31
votes
3answers
2k views

Python Forgiveness vs. Permission and Duck Typing

In Python, I often hear that it is better to "beg forgiveness" (exception catching) instead of "ask permission" (type/condition checking). In regards to enforcing duck typing in Python, is this try: ...
19
votes
12answers
2k views

How to teach Exception Handling for New Programmers?

How do you go about teaching Exception Handling to Programmers. All other things are taught easily - Data Structures, ASP.NET, WinForms, WPF, WCF - you name it, everything can be taught easily. With ...
29
votes
9answers
8k 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 ...
18
votes
1answer
1k 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 ...
4
votes
3answers
468 views

Improving exception handling? [duplicate]

I am a newbie programmer and I recently started learning about exception handling in Java. I know what try, catch and finally blocks do, but I really need to understand how to use them well and where ...
1
vote
2answers
1k views

Exception hierarchy design

In my company we are building a webapp containing serveral central services that we design ourselves and then specify as interfaces. I.e. the interfaces are application specific and they are then ...
21
votes
2answers
1k views

Who designed exceptions?

Where did exceptions and exception handling come from? I like how .NET uses it, I like how C++ supports it (but libraries unfortunately use return code or is written in C instead). I know its pretty ...
7
votes
6answers
2k views

Do you handle Out-Of-Memory conditions?

What do you do when malloc returns 0 or new throws exception? Just halt or try to survive OOM condition/save the user's work?
6
votes
6answers
1k views

If the model is validating the data, shouldn't it throw exceptions on bad input?

Reading this SO question it seems that throwing exceptions for validating user input is frowned upon. But who should validate this data? In my applications, all validations are done in the business ...
10
votes
4answers
1k views

Do you throw an argumentexception or argumentnullexception from private methods?

I was just reviewing some code I wrote a while back, and can see that I have a couple of private methods that throw argumentnullexceptions and/or argumentexceptions if there are issues with the ...
2
votes
1answer
91 views

What are possible/useful implementations when extending exceptions?

I have seen a lot of exceptions - custom or integrated in the language (PHP) itself that have no differences but their class name. When extending default exceptions to use within your own code, are ...
26
votes
5answers
2k views

Is use of finally clause for doing work after return bad style/dangerous?

As part of writing an Iterator, I found myself writing the following piece of code (stripping error handling) public T next() { try { return next; } finally { next = ...
35
votes
12answers
2k views

Why are errors named as “Exception” but not as “Error” in programming languages?

I've been thinking about that for quite a while actually. I am not a native english speaker myself but still I have years of programming experience and I always asked me this. Why is it named as ...
6
votes
5answers
667 views

Checked vs Unchecked vs No Exception… A best practice of contrary beliefs

There are many requirements needed for a system to properly convey and handle exceptions. There are also many options for a language to choose from to implement the concept. Requirements for ...
21
votes
3answers
880 views

Why is Option/Maybe considered a good idea and checked exceptions are not?

Some programming languages like e.g. Scala have the concept of Option types (also called Maybe), which can either contain a value or not. From what I've read about them they are considered widely to ...
19
votes
2answers
1k views

What is the advantage of wrapping exceptions [duplicate]

It's very common in .NET for an exception to be wrapped in several layers of "outer exceptions" which give marginally more contextual data. For example, in EF if your update fails, you get exceptions ...
12
votes
6answers
2k views

What's the best way to manage error logging for exceptions?

Introduction If an error occurs on a website or system, it is of course useful to log it, and show the user a polite message with a reference code for the error. And if you have lots of systems, you ...
10
votes
2answers
206 views

What would be the best way to handle errors in parallel programs?

With parallel algorithms knocking at the door, it might be a good time to think about error handling. So at first there were error codes. Those sucked. It was free to ignore them, so you could fail ...
8
votes
3answers
1k views

Exceptions or Error codes

We are building a web service(SOAP, .Net) which would be talking to (mostly) native clients (windows, C++) and we are wondering what is the best way to communicate errors to the client (e.g. ...
19
votes
13answers
3k views

Why are null references shunned while throwing exceptions is considered okay?

I don't quite understand the consistent bashing of null references by some programming language folks. What's so bad about them? If I request read access to a file that doesn't exist then I'm ...
11
votes
3answers
1k views

How did the idea of Exception-handling emerge into programming languages? [duplicate]

Possible Duplicate: Who designed exceptions? Today if we look at any language, exception handling is almost a compulsory feature. The languages that didn't have it earlier are implementing ...
8
votes
2answers
390 views

How can I debug exceptions that are not easily reproducible and only occur in a production environment?

I am working on an issue where the exception only occurs in our production environment. I don't have access to these environments, nor do I know what this exception means. Looking at the error ...
8
votes
5answers
601 views

How much redundancy/robustness should complex software implement?

The focus of this question: Some software performs "extra work" in order to increase the chance of a "eventually successful/satisfactory" outcome, despite one or more internal errors in the software, ...
1
vote
1answer
111 views

Pros and cons of custom exceptions [duplicate]

This may be an pointless question based on opinion, style, and context but I've been racking my brain trying to decide whether or not to make use of custom exceptions and would love to hear some pros ...
1
vote
2answers
262 views

Wrapping specific checked exception in domain unchecked ones? [duplicate]

Uncle Bob says in Clean Code book that Unchecked Exceptions should be used. Now JDK has some checked exceptions: IOException, IllegalAccessException etc. which cannot be avoided. In my application ...