Let us see an example of its use.
Let us imagine that the temperature reaches a critical level.
We could imagine throwing such an exception
of this newly-defined type
by initializing it with the abnormal obtained temperature
and a particular order to follow
when this temperature is encountered.
Thus, the intercepting block
will be able to provide several interesting informations
on the nature of the exception.
It will be able to report the abnormal obtained temperature.
Then, it will, for example here, give instructions
on what to do regarding this temperature here.
We could also imagine giving more informative messages.
For example here, we have obtained a temperature of
150 degrees [TN: Celcius, which is here supposed to be too high].
In this case, the order is to verify our meter.
Voilà! We are just about done with the complements
we wish to share on exceptions in Java.
To close this chapter, we will simply review
the little introductory example, coded in Java this time.
This is our program tasked with
printing the inverse of a few measured temperatures.
Let us begin with the coding of the main program
the way we can imagine it in Java.
The main program will store a few measures
in a dynamic array of doubles.
Those temperatures are aquired through a method
tasked with the filling of the array.
Now, we wish to print the inverses of these temperatures
stored in the array.
If the array of measures contains null values,
the printing of these temperatures' inverses will result in an error.
However, we wish to give a few chances
to the program user.
Thus, we will let him input the array with new measures
if an abnormal situation has occured.
We will grant them only two trials
to try and input a new array that does not contain any null values.
The idea is the following :
we know that the call to this method may result
in the triggering of an exception.
We will indicate that this instruction is a block receptive to errors.
Thus, we will frame it with a "try" block.
If the array of measures contains a null measure,
this method's execution will trigger an exception.
Let's assume that this exception's type is "ArithmeticException".
It will then be caught by a corresponding "catch" block.
We have not yet reached the maximum amount of trials,
we will simply inform that the user that they are to reinput the values.
Otherwise, we will tell them,
with another message, that they have ran out of trials,
and that we will consequently give up on aquiring the measures.
We thus close the execution of this "catch" block.
the execution continues sequentially
until it reaches the evaluation
of the "while" continuation condition.
The "while" will keep looping
if the number of attempts has not yet been reached.
This will make it possible to reinput the array of measures
as long as the maximum number of attempts has not been reached.
The method tasked with printing the inverses of the temperatures
thus takes an array of temperatures as parameter.
It will iterate the elements in this array.
For each index 'i', it will print
the inverse of the temperature stored at position 'i'.
Since the computation of the inverse can throw an exception
(here, we suppose its type to be "ArithmeticException" ),
we take care to indicate that this instruction
is an error-receptive block; thus framed with a "try" block".
This will make it possible to report that the problem
has occured with the index 'i'.
Since, at this level, we do not know how to solve this problem
completely, we will rethrow the exception.
Here, we rethrow the exception just as we have received it.
We could have created another one.
By the way, you may have noticed
that we have declared the exception potentially thrown
in "plotTempInverse".
Was that truly mandatory here?
Well, in fact it is not compulsory to declare the exception thrown here.
Why? Because "ArithmeticException"
is of the type "RunTimeException",
and is therefore not subject to the rule "handle or declare".
Here, declaring it simply provides a little more information.
Someone reading this method's header
will see that it can throw an "ArithmeticException",
thus taking measures accordingly.
Now, let us go all the way for this example.
We will suppose that there is a drawing method
stricto sensu, able to draw a point.
We thus imagine that there is a graphical method,
able to fulfill this processing.
Of course, we also have our famous method "inverse"
which returns the inverse of a number.
We have also chosen, here, to declare that it throws an "ArithmeticException".
Once again, it is not necessary since it is a "RunTimeException".
Now, if the given number is zero,
this method will thow an exception of the type "ArithmeticException"
with an appropriate message.
The execution stream is the following.
Let us imagine that there is a null value in our array of measures.
Now, we will thus call "plotTempInverse",