Exception (Java)

Exception (Java)

https://docs.oracle.com/javase/8/docs/api/
In practising of code you must have met this red message! Its name is Exception. It has a lot of appearance but you need to understand why Exception appear to fully catch information throws to you by the computer.

Ok now we can begin:

First of all:
	How to keep calm?
Breath and read calmly all information throw at you.
	How to use?
It's written just after that.

Is it ok ? Now we can finally use it !

First step and first example.
Take a code where it asks for birthday.
You can write a birthday like what's you have been asking for or you can do like Paul and writes his name on the sentence.
Yes Paul, it's your first red message. Keep calm, more will appear soon. So, to understand this message: The Exception name is :“InputMismatchException” Program wait an “int” but when the program reads the input return it's a String “Paul”.

First possibility to do something with this:

Just leave exceptions without more focus.
You just have to write  “throws Exception” at the end of your method.
Traduction of this is : It's possible to throw exception, but it's ok.
You see Paul, it's not a big deal.

Second Possibility : trick or treat?

Delete the last words written and continue.
So let's try with Try and Catch.
These two are bigs and strongs.
Try throws instructions.
And when it's time to interact, if Java meet something wrong, Catch appeares and Java gives instructions to Catch when catch match with the message deliver. At his turn, Catch throws instruction.
First step :
Add the exception throws. We will catch you red, at the top!
import java.util.InputMismatchException;
Then in the Catch we can write his name: InputMismatchException, caution doesn't forget “e” or just something for you with a precise name.
This gives to us:
OK! Look Paul. All lines disappear. Just one, we throw it to alert users. Like in Else if the program is stopped.
To catch a lot of exceptions, we can use more catch.
By convention, we organise catch from more probabilities to less.

Third Possibility: meet his mother

Exceptions have a mother class “Exception”. She is the last catch possible. To fully catch all the family if you don't want to treat precisely an Exception child, you have to call her at the final catch.
Like this:
She scares me. And you Paul... Paul?

Fourth Possibility: finally!

If you just want to share a message with user no matter what all throws you can use “finally” to be sure to throw instruction to the user.
This looks like:
And this if you run with it:
Yeah, I know, it's cool. Look at this Paul!
              How works Exceptions?

Yes, Paul, all these red notices it's call pile!
If you climb on the pile you can meet the problem's origin.
Functions before, permit us to delet this pile. Now we want to show them by force.
No Paul! Don't run it's not necessary...
To bottom from top we can read the “parse” in “int” doesn't work (in two lines) then he has read it without particularity but he meets problems. So he “throwFor” and climb at the top to give it to Virtual Java. He is nice.
This gift is readable! You can use the method “getMessage” to do this.
We use his nickname when you give it to him Paul!
e.getMessage();
And we can do something else :
System.out.println(“Erreur: ”+e.getMessage());
And again, more powerful:
e.getCause().getMessage();
To throw the root of this problem.
                    Hierarchy

We said it before. It's a large family, calm and… But it's seems to be really big!
For example, we can see the family in action, and this is how it works:

(To the global.)
Object
Throwable
Exception
RuntimeException
NoSuchElementException
InputMismatchException
(From the precisions.)

Throwable is the main class for errors' treatment.

Yes Paul we can do it:
               We can Generate Our exceptions

See. No problem. Deal with it. Literally.
Generate his own exception:
This just needs to write the word throw.
But you must write it in the declaration of your method.
His own exception is conventionally written with the term “Exception” inside of the new class.

Paul you turn blue, stop it now! Breath!

And:

                            finally
{
3 exceptions types:

Error: it's an environment problem. It's a heritage from class Error.
RuntimeException: programmations errors. It's a heritage from class RuntimeException .
Checked exception : all other exceptions.
}
Thanks for reading.
Thanks to FormationVideo to let me understand this.
And thanks to Jean-Michel DOUDOUX for his web site clarifications.

You can leave now Paul, it's ok!
Bye. 

Pierre Baudino

0
0

Laisser un commentaire