
Java Tutorial: How to Create an Exception Class
Java Tutorial: How to Create an Exception Class
This video goes over:
* Exception class hierarchy
* the difference between checked and unchecked exceptions
* How to create your own exception class
* How to throw an exception
* The difference between throws and throw keywords.
Content
0.96 -> hi guys in this video i go over
2.96 -> exceptions
4.24 -> in particular i explain the exception
6.879 -> hierarchy
7.839 -> and i'll demonstrate how we can create a
9.92 -> custom exception class
11.519 -> and use the throws and throw keywords to
14.32 -> throw an
14.88 -> instance of an exception now let's take
18.16 -> a look at the exception hierarchy
20.56 -> at the very top is a class named
22.32 -> throwable that directly inherits from
25 -> java.lang.object
26.8 -> this class has two subclasses named
29.199 -> error
30 -> and exception an error represents an
33.92 -> unexpected event in a program that a
36.559 -> reasonable program cannot recover
38.64 -> from that means that program would
41.76 -> typically
42.48 -> crash and fail on the other hand is the
45.84 -> exception class
47.68 -> an exception represents an unexpected
50.16 -> event
51.199 -> that is not part of the regular flow of
53.199 -> the program
55.039 -> but that a program can handle and
57.44 -> recover from
59.039 -> now there are many different types of
60.719 -> exceptions
62.239 -> and each exception type is represented
65.04 -> by a
65.519 -> subclass of exception
70.32 -> now these exception classes can be
72.96 -> categorized in two
74.64 -> types checked exceptions and
78.32 -> unchecked exceptions
81.92 -> checked exceptions are exceptions that
84.24 -> are checked
84.96 -> by the compiler hence the name checked
87.92 -> that means when we have source code and
89.6 -> we compile it
91.2 -> the compiler will make sure that we
93.28 -> handle these exceptions
95.759 -> unchecked exceptions are not checked by
98.24 -> the compiler
100.24 -> that means our com our source code
102.56 -> compiles fine
104 -> and when we run the program all of a
105.759 -> sudden an exception is thrown
107.36 -> and we may not handle that exception
112.64 -> now checked exceptions are represented
115.2 -> by any subclass
116.399 -> of exception it can be a direct subclass
120 -> or an sds subclass
122.399 -> that is not the runtime exception and
126 -> not
126.479 -> a subclass of runtime exception so
130.08 -> any unchecked exception is a subclass of
133.92 -> checked exception
137.28 -> so if we want to create a new custom
139.599 -> exception class
140.72 -> it should be a child of exception really
144 -> we want to create
146 -> a a new class that is an unchecked
149.68 -> exception now let's
153.04 -> go over an example let's say
156.879 -> we have an input we have a function that
160.56 -> reads input
162.879 -> i call this read h and here we want to
165.76 -> return
166.319 -> a valid h a valid h is anything that is
170.4 -> between
171.12 -> 0 and 100.
178 -> now i create the scanner class so i can
179.84 -> read that input in
181.84 -> i output a prompt
187.44 -> and i read that
191.2 -> h into a their site named variable named
194.159 -> h
195.36 -> and then i return that variable now i
197.84 -> have not created any exceptions yet
204 -> next i call that method
207.519 -> read h from the main and i output the h
214.08 -> so i keep this example very simple
220.159 -> now i mentioned i want to create a
221.84 -> custom exception class
223.36 -> that is phone when an h below 0
226.72 -> or above 100 is entered
230.879 -> i can name this class
235.12 -> invalid h exception now looking at the
238.48 -> hierarchy
239.28 -> i want to create a checked exception
242.319 -> most of the time when you create a new
244.56 -> exception class you always want to
245.84 -> create a checked exception
247.36 -> that is done by inheriting from
250.239 -> exception
251.599 -> so i add the extends keyword and
254.4 -> exception
257.12 -> and that is all i need to do i don't
259.44 -> have to
260.479 -> add anything else i will revisit this
263.199 -> later how we can refine it but i will
265.12 -> leave it
265.68 -> like this for now
268.88 -> next i do a condition here i check if
271.759 -> the age
272.8 -> is less than zero or if the age
276.4 -> is above 100.
279.52 -> in that case i want to throw a new
282.16 -> exception
283.04 -> so i call thrall and i don't have an
286.72 -> exception yet so i need to create an
288.4 -> instance
289.44 -> of this class so i call the constructor
293.12 -> invalid age exception
298.72 -> now this gives me a compiler error
301.36 -> because it is a
302.479 -> checked exception so now the compiler
304.88 -> checks and makes sure
306.08 -> that this is handled by my program
309.44 -> in this case i add the flows keyboard
313.44 -> notice that this is a throws with an s
316.639 -> and this is throw without an s
319.84 -> flow is a command we throw a new new
322.96 -> exception
324 -> throws simply indicates that this method
327.68 -> may throw the following exception there
330.4 -> could be more than one
331.919 -> as a comma delimited list
335.28 -> when i call this method here read age
338.56 -> the compiler again checks unhandled
341.68 -> exception type
342.639 -> invalid age exception so it's a checked
345.6 -> exception and the compiler tells me
348.08 -> i either have to add the throws
349.759 -> declaration to this method as well
352.479 -> indicating this this method may throw
355.199 -> that type of exception
357.12 -> but since we're in the main it would be
358.96 -> appropriate to add a try catch block
361.84 -> so i can click surround try catch block
367.36 -> now i cannot really execute the
369.36 -> system.out print line
370.8 -> with the h if i didn't get that h as a
373.68 -> return
374.8 -> so i add both these statements inside
377.44 -> the try catch block
382.56 -> and i handle the exception in the try
384.639 -> catch block
387.039 -> now let's run this program
390.8 -> let's say i enter 15. this will work
393.6 -> fine because it's
394.639 -> in the valid range of ages
397.919 -> now let's try something invalid
401.36 -> let's say i enter negative 15.
404.88 -> this will now follow an exception
406.8 -> invalid h
408.08 -> exception it is caught by the program
411.84 -> and here i simply output the stack trace
414.96 -> now the stack trace tells me exactly
416.8 -> where this
418.24 -> exception was thrown so i can see click
421.36 -> on this line
422.72 -> it was thrown here on this method call
424.88 -> and if i drill deeper
427.12 -> i can see the line 38 this is where
430.24 -> the exception originated now if i don't
433.919 -> want to output
434.88 -> the stack trace i can also output
438.72 -> a message invalid h for example
443.44 -> now running this again would then output
447.44 -> a message so for example 110
450.72 -> it would output invalid age
455.44 -> now we can refine this many times we
457.919 -> want to
458.479 -> create a custom message to the exception
463.12 -> that can be done by adding a constructor
468.879 -> with a parameter named message
473.039 -> and this message can then be passed to
475.199 -> the super constructor
478.96 -> so the exception class always has a
480.72 -> constructor that expects a
483.44 -> single argument as string
487.52 -> with a message and here i can then add
490.4 -> that message
492.4 -> for example h is
496.479 -> not valid so i can the the h
500.24 -> is very specific to this particular
502.4 -> instance so i can
503.84 -> provide a customized message when
505.919 -> throwing this exception
508.319 -> and here i can output it
511.68 -> let's say output error and then i want
515.12 -> to get the message
516.399 -> get message
520.399 -> need the plus before
524.08 -> and now running this
529.279 -> when i enter something negative 50
533.12 -> i get error negative 50 is not valid so
536.32 -> how did this work
538.64 -> it entered the program read in negative
540.8 -> 50 negative 50 is
542.64 -> outside the valid range so it entered
545.44 -> the then block
547.12 -> to this line it called the new
551.839 -> operator so it creates a new object
555.36 -> and then called the ins constructor to
557.36 -> initialize it
558.48 -> and it passes the string with the age
561.6 -> to the constructor so negative 50 is not
564.56 -> valid
565.36 -> this is passed to the constructor this
567.6 -> constructor
568.88 -> delegates it to the superconstructor of
571.36 -> the exception class
573.04 -> and the exception class stores it then
576.08 -> the throw keyword
577.519 -> throws the exception it gets thrown it
580.24 -> matches
580.88 -> to the catch block so it enters the
583.36 -> catch block here and executes the
585.04 -> statements
585.76 -> in the catch block now here
588.88 -> it calls the exception get message
592.08 -> this message is defined in the super
594.399 -> class exception
596.08 -> so we don't see it here but it is
598.08 -> inherited
599.12 -> so we can take advantage of it and call
601.279 -> it here
602.959 -> and then we output the customized
604.56 -> exception and the program would continue
606.8 -> if there's any other code
608.32 -> it would simply continue
612.24 -> now this gives you an idea how you can
614.24 -> create
616.16 -> customized exception classes and how you
618.48 -> can throw
619.279 -> exceptions and use the thoughts keywords
621.92 -> to declare
622.8 -> methods that may throw an exception
626 -> and that allows the compiler to check
628 -> that you properly handle these
629.519 -> exceptions
630.32 -> with a try and a catch block
633.68 -> also remember the hierarchy of exception
636.64 -> classes
638 -> and that there are two types of
639.279 -> exceptions checked exceptions
641.04 -> and unchecked exceptions i hope you
643.68 -> enjoyed this video
645.2 -> thank you for watching
Source: https://www.youtube.com/watch?v=G5n7HoClEpE