Justification for Java Exceptions

This time a rather theoretical question. Therefore, I use this function in Eclipse:

CsvReader csv = new CsvReader("src/maindroite.csv"); 

Which cannot be executed because "Unhandled exception type FileNotFoundException". Well, I understand that I need to add something for the case when the file does not exist, and at this point I usually add a few lines to throw an exception and throw it. But my question is: why do I need to catch the exception , even if the file exists ? And actually, why do I even have this thing Exceptions for some functions, and not for others?

For example, let's say I'm trying to run:

 ImageIcon icon1 = new ImageIcon("src/square.jpg"); ImageIcon icon2 = new ImageIcon("src/circle.jpg"); 

Where "square.jpg" exists, but not "circle.jpg". The program will create icon1, but not icon2, because it cannot. But I do not need to add an ExceptionHandler for the case when the image does not exist. What is the difference between both functions?

Summarizing:

  • Why do I need to add an ExceptionHandler when a do file exists?
  • Why should I add an ExceptionHandler for some functions and not others?

Thanks!

+4
source share
6 answers

Why do I need to add an ExceptionHandler when a file exists?

Basically, you need to add it regardless of the fact that you cannot write conditional code, for example, because there is no way for the compiler to know before execution if the file exists or not, so the compiler forces you to put a try / catch block, as FileNotFoundException is a noted exception.

Why do I need to add an ExceptionHandler for some functions and not others?

You only need to add try / catch blocks to everything that throws a checked exception, that is, something that does ** NOT * inherit from the RuntimeException or Error classes. The Error and RuntimeException subclasses are not checked exceptions, and you can either put try / catch or not a compiler, this does not bother. Since the constructor for ImageIcon does not throw any exceptions and simply returns null if the image does not exist, there is no need to do a try / catch block. *

+4
source

Even if the file exists now, it may not exist on your system later. Or you can give this code to someone who does not have src/square.jpg . Or maybe a hardware malfunction occurs when something on your hard drive gets damaged and accidentally deletes src / square.jpg. Perhaps the user can simply delete the files.

Exception handling in Java makes you think about what will happen in the worst case if something really bad happens (for example, src/square.jpg missing). Did you collapse Is it possible to continue as if nothing had happened? You can decide how to handle these rejection modes in a catch clause.

Some functions do not require you to handle exceptions, because functions that you would reasonably expect had nothing to make a mistake.

+2
source

1) Why do I need to add an ExceptionHandler when the file exists?

Since the Java compiler cannot know whether this file will really exist in some arbitrary runtime. (Suppose the file was deleted after compilation, but before you started the program?) In principle, your code should always have the logic necessary to handle conditions that are not expected, but can in any case.

2) Why do I need to add an ExceptionHandler for some functions and not others?

There may be various reasons, but here you probably see. Exceptions are thrown by the method, but fall on the try / catch block or on one method if they propagate up. In your example, you can wrap each call to the ImageIcon constructor in your own try / catch block, or both together, depending on what you want to do:

 try { icon1 = new ImageIcon("f1.jpg"); } catch (Exception e) { /* Handle the case for missing "f1.jpg". */ } try { icon2 = new ImageIcon("f2.jpg"); } catch (Exception e) { /* Handle the case for missing "f2.jpg". */ } 

Compared with:

 try { icon1 = new ImageIcon("f1.jpg"); icon2 = new ImageIcon("f2.jpg"); } catch (Exception e) { /* Handle the case for missing "f1" or "f2". */ } 
+1
source

Because Java distinguishes between what are known as “checked” and “unverified” exceptions. This is the source of much heated debate about whether there should be exceptions without a mark or not, and whether API methods should throw exceptions.

According to Java Trails:

Here's a guide to the bottom line: if a client can reasonably expect it to recover from an exception, make it a checked exception. If the client cannot do anything to recover from the exception, make an exception.

This is the so-called rationale.

Read more about the "dispute" here .

+1
source

Why do I need to add an ExceptionHandler when a file exists?

At the moment there is, but there is no guarantee that he will be there every time. It can happen due to many reasons. If there is no file, it makes no sense to do an InputStream and related business logic, and it is sure that your business will not be successful.

Why do I need to add an ExceptionHandler for some functions and not others?

Some classes, such as Imageicon, are not indicators for your business processing. If they are not there, everything is in order, you can still continue your basic business logic.

I feel this is the main reason why some class mandate exceptions and some not.

0
source

The idea behind checked exceptions is that they let the calling function know which exceptions can escape from it. You do not need to actually catch a FileNotFoundException if you don't want to, if you add a throws FileNotFoundException to your method signature so that your caller knows that such an exception can come out of your method.

The concept is good, but unfortunately there is no short way to state that you want to catch all exceptions that are not overly serious and wrap them in a general type of exception for your caller. In many cases, when an exception occurs, the real message you want to pass is either "the method is not completed, but the system does not seem to be on, and any side effects have been canceled" or "the method has not completed, and the system seems to didn't catch fire, but there could be other side effects. " It would be useful if there was a brief syntax to indicate the regions of the code where exceptions should be caught and wrapped in one of the above formats, but, alas, no.

0
source

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


All Articles