When do you write exception handlers?

At what point during development do you usually implement your exception handlers? Do you write them at the same time as writing the surrounding code, or do you write your own code, and then return to "hardening" it later?

I usually do the latter so that I can see exactly where and how my code crashes, but I worry that my code is not as stable as I could if I wrote exception handlers right away.

At the same time, I don’t want to spend a whole bunch of development time figuring out all the possible ways my code might fail when there are other implementation details that I haven't installed yet.

I'm curious how other developers do it.

Update: I just wanted to thank everyone for their answers!

+4
source share
10 answers

I either write exception handlers right away or let the exceptions propagate up. I'm a big fan of what I call "You don't want to come back and fix it later, right?" principle. You say you will, but to be honest, as soon as you earn it, you will not return and correct it later, will you? Just right now! Write your exception handlers right away or add a throws and make it someone else's problem. Do the right thing right now.

But you know that, sometimes you cannot. No matter what you do, do not swallow exceptions with empty exception handlers! This evil:

 try { connection.close(); } catch (Exception e) { // TODO Auto-generated code } 

I am throwing someone from my team that is checking this.

If you really don't know what to do with the exception, and you can't add a throws to spread it up, at least do something half responsible. Print a stack trace if nothing else. This is not ideal, but at least you do not hide the mistakes.

 catch (IOException exception) { exception.printStackTrace(); } 

Logging through the application logging system is better, although you should not get used to it. He must be responsible for the calls to deal with such things.

 catch (IOException exception) { log.error(exception, "Unable to open configuration file %s.", fileName); } 

As a last resort, you can execute the end of your throws by wrapping your exception in a RuntimeException . At the very least, you give someone higher up in the call chain a chance to handle the error, which is usually the right thing to do.

 catch (IOException exception) { throw new RuntimeException(exception); } 
+6
source

If I call the API, then I look at what exceptions can be thrown and resolved based on a list. Exceptions that can be thrown usually fall into categories:

  • Incredibly, in my opinion, this will be thrown - make sure the code works beautifully
  • It is realistic that it will be thrown - what if it is caused?
  • It is certain that this will be called based on the current input - add a check on the input to stop receiving it.
  • Can I raise a more relevant exception? - if the exception is likely to receive a call, will it be clear to another call code if I create a new / different exception.

In general, I believe that it is always good practice to catch all attempts to block blocks in the call stack that can catch common exceptions (Throwable), and then report this to the user - perhaps with an interface that will then send an error and stacktrace email for development teams and request user comments.

+1
source

In my exception handler, I usually raise a higher level exception. For example, when parsing a file in Python, some operations with strings, lists, and dict can result in a ValueError, IndexError, or KeyError. These exceptions are usually not useful to the caller, so I write an exception handler that raises a descriptive MyParseError. I do this at the same time when writing a method, but later, when you write a test, I sometimes make the exception message more detailed.

+1
source

during development when:

  • a unit test required
  • when presentation / persistence code is required

EDIT

in Java sometimes , you have to take care of error handling at a very early stage ( checked exceptions ) and sometimes , it is very annoying.

0
source

Sometimes both. In some cases, I know about exceptions that can be thrown, and I want to handle it when I write code, and so I write handlers right there and there. In other cases, I do not know about all the exceptions and find them later, either through documentation, testing, or both.

0
source

This is a combination of both. There are things that, as I know, can go wrong, like connecting to a database, configuring settings, reading / writing files, and also red flags from functional / technical specifications. I am trying to configure try / catch for ASAP.

As the application gets larger over time, I begin to see patterns and trends both with how the user uses the application and with how I and / or the team developed it, and add these attempts / catches as needed .

0
source

It depends on the nature of the project you are working on. In my case, if I am familiar with the logic of the system, I must know where and how to handle exceptions even before writing the code. On the other hand, I would write my material, test it, and then write handlers.

0
source

My approach is to immediately eliminate exception handling, as this is not some kind of pointless load that you can happily put aside.

Just handle the exceptions that apply in the place where you write your code, distribute all those that do not matter, and come back later to fix anything that is broken and save you a lot of tears.

0
source

As a rule, not only do I write my exception handling when I write code, but I try to write code to avoid exceptions in the first place. The advantages are that if I know that I need to handle the exception that I remember, and if I can avoid the exception, which is always a plus. I also check my code after I wrote it using the boundary conditions to see if there are any possible exceptions that I may have missed.

0
source

Writing handlers to write the actual code is the best habit that I think of because you are very free from crashes that can happen, although you can add others when you discover them.

Exception handling can be tedious the first time, but it will save a lot of time when debugging for some error. It is supported.

0
source

Source: https://habr.com/ru/post/1286502/


All Articles