When should I use Debug.Assert()?

In Debugging Microsoft .NET 2.0 Applications John Robbins has a big section on assertions. His main points are: Assert liberally. You can never have too many assertions. Assertions don’t replace exceptions. Exceptions cover the things your code demands; assertions cover the things it assumes. A well-written assertion can tell you not just what happened and … Read more

Java 8 Supplier Exception handling with CompletableFuture

The factory methods using the standard functional interfaces aren’t helpful when you want to handle checked exceptions. When you insert code catching the exception into the lambda expression, you have the problem that the catch clause needs the CompletableFuture instance to set the exception while the factory method needs the Supplier, chicken-and-egg. You could use … Read more

How to handle DataIntegrityViolationException in Spring?

The problem with showing user-friendly messages in the case of constraint violation is that the constraint name is lost when Hibernate’s ConstraintViolationException is being translated into Spring’s DataIntegrityViolationException. However, you can customize this translation logic. If you use LocalSessionFactoryBean to access Hibernate, you can supply it with a custom SQLExceptionTranslator (see LocalSessionFactoryBean.jdbcExceptionTranslator). This exception translator … Read more

‘sys.excepthook’ and threading

I just stumbled over this problem and as it turns out, it was the right time to do so. New in version 3.8: threading.excepthook Handle uncaught exception raised by Thread.run(). The args argument has the following attributes: exc_type: Exception type. exc_value: Exception value, can be None. exc_traceback: Exception traceback, can be None. thread: Thread which … Read more

Why are Exceptions said to be so bad for Input Validation?

Reading these answers, I find it very unhelpful to say, “Exceptions should only be used for exceptional conditions”. This begs the whole question of what is an “exceptional condition”. This is a subjective term, the best definition of which is “any condition that your normal logic flow doesn’t deal with”. In other words, an exceptional … Read more

Extract traceback info from an exception object

The answer to this question depends on the version of Python you’re using. In Python 3 It’s simple: exceptions come equipped with a __traceback__ attribute that contains the traceback. This attribute is also writable, and can be conveniently set using the with_traceback method of exceptions: raise Exception(“foo occurred”).with_traceback(tracebackobj) These features are minimally described as part … Read more