Error logging and exception handling that you can't often see in the Java area

For example, this is no exception and is out of the question.

Why not?

In the first place, the mechanism called "exception" was created to enable the description ** "What should I do when the process is skipped for this reason?" ** (I don't think I'm angry). .. The "exception" that is sent is exactly that "reason for this and that", and even if it is said that "the processing was blown away for some general reason", it can only be returned as "foolish". It's **.

Okay, then should we classify each reason like that and create an exception class?

not good. Of course I'm not saying it's totally useless, but ** I have something to do before that **.

Responsibility decision

** Decide "who, how far, what to do". ** ** For example, suppose there is an operation of "reading a file, parsing it, and holding it as an object". At this time (although it is a little detailed), four characters appear as characters: "the one who passes the location of a certain file", "the one who reads a certain file", "the one who parses what is read", and "the one who holds it".

What and how much should these guys do? Common sense suggests that "the one who passes the location of a file" is at least "what would be the location of the file", "(probably) in a string", and "without loss". There will be. Based on this assumption, "the one who reads a certain file" does not have to think about "does something like the location of this file really indicate the location of the file?" Even if you go to read and there is no file, you can complain (= exception), "Hey, I went to look for the information you gave me, didn't I ?!".

To put it coolly, it's called CoC, ** Design by contract **. What, "Isn't there an exception!"? What are you talking about ** I'm saying that the exception is for use when the contract isn't kept **.

Utilization of existing things

** If the argument is incorrect, be quiet and throw an IllegalArgumentException **. that's all.

...... It's too messy, so I'll dig a little deeper. For example, suppose you prepare the following API. It takes two arguments and returns the result of a / b. (It's just an example, so please forgive me for being too much of a substitute)

public int div(int a, int b){
    return a/b;

However, it was criticized that this was not good. This is because div (1,0) will cause an ArithmeticException (it may be better to say DivideByZeroException to the people of .net, I am also a DBZE sect ...).

What would you do then? Well, we'll have validation.

The answer is, of course, "** in some cases **". This should be created if the intended use is to "catch individual exceptions together to some extent", as in the previous example. It's still unsightly to catch a lot of exceptions. Conversely, if you have no reason to make something **, you should use the existing one **. It's not difficult. In short, reinventing the wheel is ridiculous, so it's just a matter of stopping.


** Keep the contract and enjoy coding! !! ** **

Also, ** I'm asking you, so don't just imitate yourself going to destroy the type system **. (However, I think this should be said to an idiot who treats code values with raw numbers ...)

Recommended Posts

Error logging and exception handling that you can't often see in the Java area
Questions in java exception handling throw and try-catch
[Ruby / namespace] Deepen ":: ← this" that you often see in Ruby
Java Error Handling Basics-The story that catch is only picked up in the foreground
Exception handling techniques in Java
I tried to summarize the words that I often see in docker-compose.yml
Leverage Either for individual exception handling in the Java Stream API
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
This and that of the implementation of date judgment within the period in Java
Find the maximum and minimum of the five numbers you entered in Java
Review of "strange Java" and Java knowledge that is often forgotten in Java Bronze
Regarding the transient modifier and serialization in Java
[Java] Handling of JavaBeans in the method chain
Recommended site for errors that you often see
[Java] The difference between the Stream.of () and () methods that you do not know unexpectedly
Java exception handling?
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling
Write ABNF in Java and pass the email address
Java classes and instances to understand in the figure
This and that for editing ini in Java. : inieditor-java
[# 2 Java] What is object-oriented programming that you often hear?
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
The story that .java is also built in Unity 2018
Syntax and exception occurrence conditions that can be used when comparing with null in Java
A program (Java) that outputs the sum of odd and even numbers in an array
What to do if you don't see the test code error message in the terminal console
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-
Causes and remedies for the exception "ServletException: Error instantiating servlet class" when deploying Java applications
Filter the Java No. 2 Optional list that was useful in business and get the first value