This question has already been answered:

At the interview I was asked the question, what is the general rule when to do try and catch. I answered when there is work with external resources, where the occurrence of an error does not depend on the programmer. For example, when there is work with files or a database (the file may not exist, the disk may run out of space, the connection to the database may fail) The answer was not complete.

Supposedly there is some rule when you need to use try catch

I myself tend to think that the correct answer is: you should use try catch, when you can’t do with if / else checks. What do you think? What is the correct answer?

The question concerns any language.

Reported as a duplicate by Grundy , PashaPash members ♦ Nov 7 '16 at 20:10 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • And the language is not Java by chance? - pavel
  • @pavel, The question concerns any language. - Grundy
  • Not java. Any language. (Although I know that in Java, I often have to process it, otherwise the compiler will not compile) - Oleg
  • @Grundy Well, every language has its own cockroaches ... In Java, checked exceptions - a separate question could be. And then IMHO code-style - pavel
  • I closed as a duplicate, because the general approach to using exceptions in Java and in C # is about the same. - PashaPash ♦

2 answers 2

I do not know what answer they wanted to hear from you at the interview, and it seems to me that there can be no absolutely correct criterion.

My opinion: you need to catch an exception in the place where you know what to do with it .

I will explain.

Suppose you have a function to open a file, a file was not found, and it throws an exception. What to do at this point?

This is an important operation, it is impossible to continue without information from the file, so you need to inform the user and end the program? Or is it a configuration file that the program does not have at the beginning of its operation, and it will need to be created with default data?

In the function of opening the file you do not know. Therefore, it is not necessary to catch an exception, let it fly to a higher level. But at the level of more large-block logic, which launched a configuration read operation, it is possible to catch an exception when opening a file and catch it and simply create a default configuration.

Such a one-tier scheme may be too simple, and you will have to repackage an exception on your way, adding meaning to it. For example, when reading a number from a string, an exception occurred because of a format mismatch. You do not know at this point whether this problem is fatal, and simply skip the exception further. The higher-level logic knows that a configuration data reading operation is taking place, which for some reason ended with an exception. At this point, the program's logic has the ability to decide that the problem cannot be ignored, and it throws a higher-level exception, which means “the configuration read has completed abnormally”. This exception, in turn, catches the top-level business logic, which informs the user about the loss of the configuration file and recreates it again.

In any case, premature catching of exceptions at a too “internal” level is harmful, because a program at such a point usually cannot do anything reasonable.

    The question is exactly worth it? try and catch ? Not exceptions at all?

    Then - there, where generation of an exception which can be reasonably processed by your code is possible. If an exception cannot be generated - try/catch to nothing :) If you can’t handle it - there’s also no point in catching it, it’s better to pass on.

    • try and catch, not exceptions at all. - Oleg
    • one
      A friend of mine said that you should use try / catch when a runtime exception may occur. I like this answer as long as I consider it the right answer. Your addition, “If you cannot process it, there’s also no point in catching it, it’s better to pass on.” I also like it - Oleg
    • @ Oleg2 the answer "if it can arise" may be simple, but it is fundamentally wrong. Look - at any object creation you can get OutOfMemoryError. Another couple of exceptions can occur at all at any time (at least in C # so, in Java, most likely, too). So ... your friend advised you to use try / catch anytime and anywhere. - PashaPash ♦
    • @ Oleg2 Tell me, what should your try / catch do in a situation where you cannot handle an exception? What should it look like? - Harry
    • @Harry then do not need to do try catch - Oleg