Antipatters exceptions

Exception handling is a challenge for new and experienced developers. What are some examples of exception handling antipatters that people have seen?

+4
source share
5 answers

Bad cleaning logic

Throwing the cleanup code from the destructor. This is doubly bad, because a.) Throwing from the destructor is usually bad and b.) Because even if you can catch it, there is nothing to be done.

File::~File() { if (!close(fd_)) { throw FileIOException("Could not close descriptor.", fd_); } } 

User interface from hell

  try { // ... lots of UI logic here ... } catch (Exception error) { alert("This program has performed an illegal operation and needs to quit."); System.exit(-1); } 

Retry without refund

  bool has_connected = false; while (!has_connected) { try { EstablishConnection(); has_connected = true; } catch (...) { // IGNORE } } 
+3
source

Here, which is not completely different from what I saw before.

 try { methodThatThrowsSomeException1(); methodThatThrowsSomeOtherException2(); methodThatThrowsSomeOtherException3(); methodThatThrowsSomeOtherException4(); methodThatThrowsSomeOtherException5(); methodThatThrowsSomeOtherException6(); ... methodThatThrowsYetAnotherException20(); } catch (Throwable e) { log.error("There was a problem reading the user role"); role = PRIVILEGED; } 
+3
source

catch (...) in C ++.

Perhaps the worst way to make your code look stable ...

The same applies to any other language where you catch exceptions that you do not expect and just swallow them silently to hide the error from the user. But (...) usually used to detect exceptions, such as dereferencing a NULL pointer or denial of access, which means that the swallowed error is likely to manifest itself later in ways that may look completely unrelated to the root of the problem.

+2
source

My biggest problem with the pet is the creation of an exception inheritance hierarchy in which offspring relationships have little effect on whether an exception should be thrown. Not much has been done for the predefined exceptions, but I prefer to avoid throwing them and instead defining new exceptions in cases where the caller must assume that the state of the system is fine, except that this is implied by the fact that the routine didnโ€™t return successfully , unlike those in which the state of the system is broken. For example, if a method should open a file and return a new document object, but there is a problem with parsing the file, it should not kill the entire application. It should inform the user that the file cannot be opened, and then continue as if the user was not trying to open the file. It does not matter why the file does not open; the question is whether the state of the application was damaged. Unfortunately, none of the standard exceptions handles this very well.

+1
source

Use of exceptions in shared libraries intended for use in different C ++ languages โ€‹โ€‹/ dialects. Since the C ++ compiler cannot guarantee that you will not accidentally throw an exception for the caller (unlike Java), you are simply setting yourself to fail.

+1
source

All Articles