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

learn more… | top users | synonyms (1)

71
votes
12answers
8k 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. ...
1
vote
1answer
53 views

How to find the cause of an exception in async code

I often see myself trying to follow my own code to find out where the exception came from. The typical example is when some parsing fails, and I catch the execption. Then I spend a ridiculous amount ...
0
votes
0answers
54 views

Is Shiro's use of Auth Exceptions an example of control flow? [duplicate]

In Java/OOP-land, it is a well established anti-pattern to use exceptions for control flow. Apache Shiro is one of the standard Java security frameworks. When using Shiro, especially when ...
25
votes
7answers
2k views

Should a C++ program catch all exceptions and prevent exceptions from bubbling up past main()?

I was once advised that a C++ program should ultimately catch all exceptions. The reasoning given at the time was essentially that programs which allow exceptions to bubble up outside of main() enter ...
12
votes
2answers
2k views

Is using Exceptions at the highest level of a program considered bad practice? [duplicate]

I have seen programs using this strategy and I have also seen posts considering this bad practice. However, the posts considering this bad practice have been written in c# or some other programming ...
1
vote
1answer
83 views

Does exception handling belong at the lowest level of the runtime?

I'm designing a fairly simplistic stack-based programming language, and implementing it in Python. (no link, because it's not fully implemented yet.) The language itself is essentially intended to be ...
0
votes
5answers
371 views

Handling DomainExceptions using REST endpoint

Suppose I'm doing some DDD. Now, I have a microservice reflecting a bounded context/a part of a bounded context. Now, suppose there is a REST endpoint: '/somedomainmodel/someaction' My API user is ...
0
votes
1answer
115 views

How can you catch all index out of range errors in python?

Our program does these kinds of operations hundreds of times for many different variables and lists, then uses them throughout the program: variable = values[5] The list values are coming from the ...
1
vote
2answers
76 views

Should a client check for persisted item existence before modifying it?

I'm developing a MVC Web application with a REST interface. The REST controller performs actions on persisted items through a service class, which translates exceptions coming from the persistence ...
8
votes
2answers
241 views

Do we need to validate entire module usage or just arguments of public methods?

I've heard that it is recommended to validate arguments of public methods: Should one check for null if he does not expect null? Should a method validate its parameters? MSDN - CA1062: Validate ...
2
votes
2answers
372 views

How to make sure that the destructor cannot throw an exception?

I know that we can specify the compiler that a function is not throwing an exception by using noexcept specifier but noxecept functions can still throw exceptions . So, how to prevent destructor from ...
3
votes
3answers
169 views

Are exceptions only for handling errors?

I have a script that throws exceptions when something goes wrong. However, for the purposes of testing I also want to capture specific points although I'm not sure whether they would be deemed errors ...
1
vote
2answers
35 views

Exception Handling for class attributes in setters and constructors [closed]

I would like to discuss a question about best practices regarding exception handling (e.g. in Java). Normally, when setting the attributes of a class, I check the arguments in the setters for ...
-3
votes
5answers
342 views

Differences between `throw` and `throw new` and exactly how exceptions “bubble up” [closed]

Can someone please explain the differences in C# between: throw throw new and exactly how exceptions "bubble up" as I've heard they do? In my daily job, I've used just try/catch to mostly ...
6
votes
1answer
289 views

Is it good practice to use NoStackTrace in scala?

I came across the NoStackTrace mixin for Exceptions in scala. Is it good practice to use it, or should it be considered "internal" to scala and left alone?
12
votes
6answers
478 views

Should I log errors on exception throwing constructors?

I was building an application for a few months and I come to realize a pattern that emerged: logger.error(ERROR_MSG); throw new Exception(ERROR_MSG); Or, when catching: try { // ...block that ...
14
votes
4answers
16k 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 ...
-2
votes
1answer
46 views

Convention for exception argument order [closed]

Just a quick question about a design pattern for creating custom exceptions. The question is more about the order of parameters. If you can specify more data in the exception, should the parameter for ...
64
votes
8answers
14k 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 ...
5
votes
2answers
199 views

How to get around non-initialized objects in the `finally` block?

I like using final variables whenever possible. Often those variables have to be closed afterwards. Note that I'm currently working on Java 6 so there is not closeable interface, but the same applies ...
4
votes
3answers
121 views

Drawbacks of a master error handler?

I'm thinking that I could simplify my life by making an exception handling class that I can just ship all exceptions to and it will handle appropriately. Ideally: def dostuff(): try: dothis() ...
20
votes
3answers
2k views

Exceptions - “what happened” vs “what to do”

We use exceptions to let the consumer of the code handle unexpected behaviour in a useful way. Usually exceptions are built around "what happened" scenario - like FileNotFound (we were unable to find ...
8
votes
2answers
426 views

Is the strong exception safety guarantee with a pass-by-value argument which can throw on destruction possible?

Suppose you have a type with a throwing destructor, and a function receiving it by value. Can that operation ever provide anything better than the basic exception guarantee? Or formulated differently, ...
4
votes
2answers
813 views

Best practice to handle POST with array of objects if at least one object raise an Exception

I want to POST an array of objects and make an insertion in database concerning each of them. What is the best practice if one object does not conform and raise an Exception? (cancel all transactions ...
7
votes
2answers
1k views

Should one derive / inherit from std::exception?

While designing my first 'serious' C++ library, I'm asking myself: Is it good style to derive ones exceptions from std::exception and it's offsprings?! Even after reading Designing exception ...
24
votes
4answers
13k views

Why shouldn't a method throw multiple types of checked exceptions?

We use SonarQube to analyse our Java code and it has this rule (set to critical): Public methods should throw at most one checked exception Using checked exceptions forces method callers to ...
11
votes
5answers
615 views

What is the conceptual difference between finally and a destructor?

First, I am well aware of Why is there no 'finally' construct in C++? but a lengthy-growing comment discussion on another question seems to warrant a separate question. Apart from the issue ...
0
votes
1answer
49 views

Include exceptions in activity diagrams? [closed]

Should I include exceptions, like a TimeOutException or just general Exceptions, to a activity diagram? For example: A activity diagram describing a client-server connection. Several exceptions can ...
1
vote
2answers
118 views

Throwing exceptions in application configuration providers

Simple question: What is the best/common practice regarding to throwing errors for application configuration providers? Given is a simple key/value-based configuration source: class Configuration ...
2
votes
3answers
95 views

Is there particular circumstance that throwing root superclass exceptions is a good practice?

I've been taught that exceptions should be have concise meanings and should contain a message that explains to the client what the exceptional situation is. I am wondering, since I found a piece of ...
-3
votes
2answers
212 views

Why ever use exception throw (in C#) except for Class Library development? [duplicate]

Why would I ever throw exceptions in code? (except for one specific scenario where I am developing a class library which the consumers of it, will not want / be able to change). To be more specific, ...
51
votes
8answers
21k views

Return magic value, throw exception or return false on failure?

I sometimes end up having to write a method or property for a class library for which it is not exceptional to have no real answer, but a failure. Something cannot be determined, is not available, not ...
1
vote
2answers
4k views

Exception Handling in Java web application

I know exception handling is a topic often discussed in the world of Java. I've read a few threads on here and SO as well. http://stackoverflow.com/questions/2169339/exception-handling-in-a-java-...
1
vote
1answer
163 views

Low cost exceptions implementation using metaprogramming [closed]

(preface - boring stuff, feel free to skip down to the implementation details) I need to provide exception handling to a language I am working on. It "compiles" to a subset of C, and since I don't ...
2
votes
2answers
101 views

How can a method handle validation and entity creation without output parameters?

I have 3 simple classes. A Reference, a Parent, and a Child. The Child knows the Reference and Parent instances it's associated with. Here they are, initialization and other data/methods omitted: ...
52
votes
4answers
29k views

Is it better to use assert or IllegalArgumentException for required method parameters?

In Java, which is more highly recommended, and why? Both types will throw exceptions, so in that regard handling them is the same. assert is slightly shorter, but I'm not sure how much that matters. ...
2
votes
0answers
71 views

Handling exceptions in multiple-issue CPUs

From what I read, VLIWs execute instructions in bundles, i.e. the CPU loads a bundle of instructions and dispatches them all at once. This is possible because the compiler scheduled instructions in ...
23
votes
2answers
1k views

Who should read Exception.Message if at all?

When designing exceptions should I write messages that a user or a developer should understand? Who should actually be the reader of exception messages? I find exception messages aren't useful at all ...
5
votes
3answers
87 views

Recommendations for adding exceptions messages to code [closed]

The "problem" that I am having right now its quite simple, I am creating a code with A LOT of validations, in case one of those fails it is supposed to throw an exception. In order to make the ...
1
vote
1answer
153 views

How often should I use try/catch statements? [duplicate]

Everything I've read says that they should be used when something that simply "shouldn't happen" does happen. But the thing is, there's a lot of things that could go wrong. For example, if I'm ...
2
votes
2answers
90 views

Advantages of extending the default Exception class

I've seen that it's possible to extend the default Exception class in PHP, enabling one to throw an IncorrectParameterTypeException exception, or a ValueOutOfRangeException exception (maybe these are ...
0
votes
1answer
132 views

Can I execute a query in a catch block of try-catch? [duplicate]

I want to confirm the right approach of using try-catch exception handler. I have written a query in a try block, and if any exception is thrown, it will execute a query in a catch block. Is this ...
34
votes
4answers
17k views

Why do iterators in Python raise an exception?

Here's the syntax for iterators in Java (somewhat similar syntax in C#): Iterator it = sequence.iterator(); while (it.hasNext()) { System.out.println(it.next()); } Which makes sense. Here's ...
4
votes
4answers
230 views

Should exceptions be raised higher up or lower down or both? [closed]

When calling some function in a Python application, the function often calls functions deeper down which again call functions deeper down, etc. It is easy to unknowlingly pass a bad value to the top-...
12
votes
5answers
2k 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 ...