Hello!
Can somebody please explain why we need an if clause for the program to verify?
Why isn't the error just thrown when we run the code?
Thanks
Andrei
Level 41
What is this task trying to teach us? I don't understand the use of the IF clause to pass the verification - throw/throwing exception in static block
Under discussion
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
7 December 2020, 12:27useful
instead of an if condition you could use
Cause the static analysis of Java isn't that good, it sees a possibility that the initialisation may finish normally. If you just throw an Exceptioin then the analysis will see, the static block never can finish normally and therefore it is not worth the effort to continue.
If you add an if clause then the analysis again comes to the conclusion that there is a code path to finish the initialisation without exceptions. And therefore you are able to validate your code. +1
Andrei
7 December 2020, 12:50
But what would the possibility be that the initialization may finish normally, when I wrote, black on white to throw an error and nothing else.
Do you know of such an example where the error might not be thrown?
Is java some kind of magic? Am I missing something? haha
0
Nouser
7 December 2020, 13:01
that would work, but only if there is no need to rethrow inside the catch block
works, cause there is a code path to finish the initialisation normally, it just continues after the catch block
this not, throwing a new RuntimeException will end the initialisation without any other possibility. Add an if clause and things look different again ;)
+1
Nouser
7 December 2020, 13:14
As said static analysis isn't that smart and suspects another code path other than the exception. It doesn't analyze the condition itself, just if one is there. So that will work as well. Even just a true without an else clause will do.
+1
Guadalupe Gagnon
7 December 2020, 15:17useful
This task is demonstrating that you can interrupt the loading of the class in the static block. By doing this then the main method would never be reached. This is an important concept when the class uses outside resources, such as databases or online resources. Let's say that the computer the code is running on doesn't have access to the internet, that would kind of be a little bit of a problem if the code expects to access its needed content from the internet.
Now, exceptions are not for the benefit of the code designer. You already know what you want the code to do, how it is going to do it, and have already worked out all the problems that would occur in the code with your exact intentions for the code. Maybe you added a little more validation to the code for anticipated problems that could occur. Exceptions are only for the other users of the code. What happens, when you release code to the public, is that other users/programmers are going to use your code in multiple ways. Think of your code as a [square], and then a user comes along and decides to use your [square] code to solve a [circle] problem. Did you design your code with the foresight that a [square] should fit into a [circle]?
We are using code from libraries that were literally writing more than half a century ago, when transistors were the way computers computed. Do you think these libraries anticipated cross-bridge platforms running fiber optic internet access so that users in one part of the globe could play games in real time virtual reality against others in any other part of the globe with no more than 30ms of lag?
So, an exception says that "I don't know how or what you are going to be doing with my code, but if this requirement is not met then this code won't work. Now, it is your job to catch the exception and figure out how to proceed with your code so that it is valid."
+3
Andrei
7 December 2020, 15:25
Hi Guadalupe, thank you for the detailed answer. I understand better the situation and the use of exceptions; what is not clear to me, in this scenario, is that although I am EXPLICITLY throwing an exception (without any way of being misinterpreted by the computer) the program does not compile and warns me that something is wrong.
EVEN THOUGH, in theory, the program should compile, run and display the error I have thrown.
My misunderstanding is why does it not compile, since, in theory, the code should compile?
+1
Guadalupe Gagnon
7 December 2020, 16:02useful
The code compiles just fine. This exception would occur before the class is fully loaded into memory because the static block is run before the constructor block (in this code it would have a default constructor). Your problem is that you are limiting your thinking of this to a very narrow understanding. This is not criticism as almost all students learning programming are afflicted by this (myself included). It is hard to imagine the wider scope of the problem when being introduced to these concepts. This is just demonstrating the interruption of loading a class, and in a real life project it could be one class in literally thousands of classes in a larger complete project.
I think the other problem that you are having is that anything that interrupts the loading of a class in Java is going to cause a ExceptionInInitializerError. This doesn't prevent compiling, it is just another type of Throwable that works like Exceptions. This is thrown by the class loader system internally. Take this as an example:
Here is an example that causes an Error to occur in the loading of a class outside the main class. It is caught successfully in this code, as you will see if you run it. Real code wouldn't have an error like this, where it always has a division by 0 occurring; but instead something more like what I explained above when resources were unreachable. In the catch block the programmer using the class could handle this problem if it occurs. You can remove the try-catch and get similar results to what you experienced before. +1
Andrei
7 December 2020, 16:28
OK, I understand better, thank you for the explanation. 👍
+2