Exception handling in Java

Exception handling in Java


Exception handling in Java

This series of videos is suitable for programmers with some programming or Java experience who want to learn object-oriented programming and dig deeper into some other Java features. This video is an introduction to exception handling in Java. This video is suitable to watch after reading Chapters 0-10 of Java, Java, Java: Object-oriented problem solving, by Morelli, Walde, and Hoffman. However, the video is very straightforward and can also stand alone or accompany any other textbook that has introduced programming in Java. Let’s have some fun learning to program in Java!

Here’s the video creating the Proverb game:
   • Three collaborating objects and the i…  

You can practice writing an exception handler that mimics the JVM exception handler here*:
https://codecheck.io/files/2211260153

You can practice writing an exception handler that invokes the no-arg constructor of the ProverbManager class here*:
https://codecheck.io/files/2211260204

The Proverb game classes are in my github repository, available here:
https://github.com/ProfessorBurke/Jav

The code from this video is in my github repository, available here:
https://github.com/ProfessorBurke/Jav

You can access the Java, Java, Java book here (there are practice exercises in the Exceptions chapter):
https://runestone.academy/ns/books/pu

If you want to earn credit for learning to code, take an online course at CCRI! These videos are part of the Advanced Java course:
https://ccri.edu/comp/

*The CodeCheck system courtesy of Cay Horstmann, https://horstmann.com/codecheck/

00:00 Throwing exceptions with a throws clause / the stack
02:32 Throwable inheritance hierarchy / checked vs. unchecked exceptions
04:01 @throws Javadoc tag
05:24 Writing a handler / catch clause


Content

2.1 -> hi this is Maggie in this video we're
4.319 -> going to talk about exceptions which are
6.66 -> a mechanism in Java for handling
8.34 -> exceptional conditions within a program
10.26 -> such as an i o problem and attempt to
13.2 -> access an array Beyond its bounds or an
15.719 -> attempt to treat a null pointer as if it
17.52 -> were an object we'll begin with what
19.619 -> happens when we add a throws Clause to a
21.9 -> method header
23.34 -> this is the proverb game.java example
26.22 -> from our video on building a set of
28.38 -> collaborating classes in Java I'll link
30.84 -> the video here if you're interested but
32.82 -> it isn't necessary to watch that video
34.559 -> to understand this one all you need to
37.02 -> know is that the proverb manager class
39.239 -> has a read Proverbs method
42.48 -> called by The Constructor that
45.18 -> instantiates a scanner by passing in a
47.76 -> file object
50.1 -> now I'm passing in a file that I created
52.68 -> and that I know exists and I'm checking
55.26 -> if the file exists before instantiating
57.48 -> the scanner in the read Proverbs method
59.64 -> so I'm going to comment out the code
61.98 -> that checks if the file exists
66 -> and I'm going to pass in the name
67.86 -> non-existent.txt
72.96 -> which is not the name of a file in this
75 -> project so that we can see the stack
76.979 -> Trace that results when the file not
78.659 -> found exception is thrown
81.6 -> so when I run the program we get the
83.4 -> error message and the stack trace the
85.979 -> error message says non-existent dot text
89.28 -> the system cannot find the file
91.02 -> specified the stack Trace shows us the
93.84 -> series of method calls that led to the
95.64 -> method that through the exception back
98.52 -> here in
100.439 -> file input stream dot open zero
103.799 -> we can see the line in each file on
106.74 -> which the calls are made
109.799 -> and in Eclipse we can click to go
112.14 -> directly to any of those methods
115.799 -> every method that invokes file input
118.38 -> stream dot open0 or any method that
121.38 -> calls that method or any method that
123.119 -> calls a method that calls that method
124.74 -> must add a throws Clause to the
126.899 -> signature of the method or one of them
128.88 -> must catch the exception here every
132 -> method has a throws clause
134.459 -> when an exception is thrown Java goes
137.099 -> through the stack the methods that were
138.54 -> called that led to the method throwing
140.16 -> the exception until it finds a Handler
142.44 -> for the exception if all of the methods
144.66 -> throw the exception then the Java
146.459 -> virtual machine handles the exception in
148.14 -> the way we see here by printing the
150.3 -> message and the stack trace this is an
152.819 -> image of the Java inheritance hierarchy
155.16 -> for throwable from oracle's website
158.16 -> there is a superclass throwable that has
160.44 -> subclasses error and exception errors
163.98 -> are serious problems that we are not
165.66 -> concerned with in ordinary programs
167.94 -> exceptions come in two basic varieties
170.099 -> checked and unchecked in this image you
173.459 -> can see the subclass runtime exception
175.62 -> which is an unchecked exception examples
178.739 -> of runtime exceptions include null
180.54 -> pointer exception index out of bounds
182.7 -> exception and arithmetic exception
185.76 -> when we are required to add a throws
187.92 -> Clause to a method header it is because
189.599 -> that method throws or invokes another
191.94 -> method that throws a checked exception
194.64 -> the checked in checked exception means
197.159 -> the compiler is checking to see if the
198.659 -> exception is either handled or
200.22 -> explicitly thrown if it isn't the
202.68 -> program won't compile the compiler does
205.2 -> not force us to acknowledge unchecked
207.12 -> exceptions in this way because unchecked
209.459 -> exceptions are less predictable and are
211.379 -> often caused by programming errors you
214.26 -> can throw and catch or handle both
216.239 -> checked and unchecked exceptions but you
218.34 -> are only required to handle checked
220.2 -> exceptions we have seen how to pass them
222.48 -> up the runtime stack with a throws
224.099 -> clause in the method signature we should
226.319 -> also document in the dabadoc that a
228.239 -> method throws an exception let's add
230.28 -> that to our read Proverbs Constructor
233.64 -> and main method javadoc comments throws
236.04 -> file not found exception thrown if the
238.319 -> Proverbs file cannot be found in the
240.06 -> default directory
250.799 -> it is important to document exceptions
252.9 -> that are thrown in a method so any part
255.36 -> of the program that calls the method
257.04 -> knows to throw or handle the exception
259.26 -> it is part of the methods interface its
261.84 -> contract with methods that use it
264 -> now we already had code that checked if
266.28 -> the file exists before trying to read
268.259 -> from it
270.24 -> if the file isn't found the proverb
272.82 -> manager had code to create an empty
274.68 -> arraylist of proverb objects
276.96 -> this isn't ideal because it doesn't let
279.18 -> the calling method know there was a
280.74 -> problem the calling method passed in a
283.02 -> file name so to complete execution as if
285.54 -> that file was read when it wasn't can
287.52 -> lead to errors let's redesign this to
289.979 -> give control over how to respond to the
292.38 -> calling method the one that passed the
294.12 -> file name in maybe a file not found is a
297.72 -> serious problem and we don't want to
299.28 -> continue under those circumstances or
301.86 -> maybe there's a contingency file and the
303.72 -> calling method might want to try again
304.979 -> with a different file maybe the calling
307.199 -> method is getting the file name from the
308.4 -> user and they need to let the user try
309.78 -> again in case it was a typo if the
311.94 -> proverb manager handles the problem then
313.979 -> the proverb game never gets a chance
316.5 -> so let's let that file not found
318.24 -> exception be thrown and let's let the
320.699 -> proverb game catch it and handle it in
323.1 -> its own way
324.36 -> first we'll remove the throws Clause
326.82 -> from the method signature
330.6 -> the main method will no longer throw an
332.82 -> exception that means we have to handle
334.62 -> it to do that we wrap the code that
336.96 -> could throw the exception in a try block
339.3 -> that code is the Constructor for the
342.06 -> proverb manager
346.32 -> so we'll write before that Constructor
348.479 -> the keyword try and then open curly
351.419 -> brace and after the Constructor we'll
353.46 -> put a closed curly brace
357.78 -> after the try block we put a catch block
360.24 -> the catch block is the keyword catch and
363.24 -> then in parentheses the type of
365.039 -> exception we're catching and a name for
367.38 -> the exception object it's standard to
370.02 -> call it e but you don't have to we're
372.36 -> catching an exception of type file not
374.28 -> found so we'll write file not found e
376.44 -> closed parenthesis open and close curly
378.96 -> braces that defines our catch block
384 -> now I should move my variable
385.5 -> declaration code for the proverb manager
387.36 -> outside of the try block so let me do
389.34 -> that
392.52 -> and eclipse is warning me that the
394.38 -> proverb manager object may not have been
397.139 -> created in the lines that follow our try
399.72 -> catch block that is because in the try
401.58 -> block if the file not found exception is
403.8 -> thrown The Constructor does not finish
405.479 -> executing control will flow to this
407.58 -> catch block as soon as the exception is
410.039 -> thrown
411.06 -> so we need to figure out how to deal
413.1 -> with this in the catch block we have
415.44 -> some options depending on whether the
417.18 -> design is still fluid and we can modify
419.22 -> the proverb manager class if we can't
421.319 -> modify the proverb manager class we have
423.12 -> the option of including the entire
424.62 -> program in the try block so that we'll
426.78 -> try to create the proverb manager object
428.4 -> from the file and if successful will
430.56 -> continue and otherwise we'll catch the
432.72 -> exception I'll show you what that looks
434.88 -> like we move all the code into the try
441.479 -> but unless we're going to do something
443.22 -> very different from the jvm in that
445.44 -> exception it's basically the same effect
447.36 -> the program crashes with an exception
449.28 -> let's mimic the jvm exception to get a
451.919 -> little practice with that exception
453.84 -> object the jvm prints the message and
457.02 -> the stack Trace we are given in the
459.66 -> catch Clause a parameter which is an
461.639 -> object with information about the error
465.06 -> we call that object e we can send
467.46 -> messages to the object so let's print
470.099 -> e.getmessage
474.78 -> and we can send the print stack Trace
477.3 -> message to E
480.66 -> when I run that
483.18 -> notice that we see the message
488.34 -> and then the print stack Trace method
490.5 -> prints both the message and the stack
492.36 -> Trace
495.539 -> now you may not want or need to interact
498 -> with the exception object but you can
500.039 -> send those messages and get that
501.539 -> information if you're logging the error
503.819 -> But continuing with execution you might
505.86 -> want to log that information
508.319 -> if our exception handling is going to be
510.539 -> the same as the jvm exception handling
512.64 -> we should just throw the exception so
515.219 -> let's try something different
517.26 -> okay so what's another option we can't
520.08 -> proceed without a proverb manager in our
522.719 -> case we're passing in a hard-coded file
525 -> name a literal string where does the
527.339 -> file come from maybe we could construct
529.86 -> or download a new file if the file has
532.08 -> been deleted or moved maybe we tell the
534.6 -> user there's been a fatal error and they
536.16 -> need to reinstall the system
537.839 -> we could display a custom message system
540.12 -> out print line the Proverbs file
542.36 -> non-existent.text has been moved or is
544.2 -> missing please restore the file or
546.54 -> reinstall the program
558.54 -> the stack Trace would not be useful to
560.459 -> the user but this message will hopefully
562.62 -> make sense let's try something else I'm
565.5 -> going to put the program code back below
567.66 -> the try block all except for the code
569.58 -> that throws the exception The
571.14 -> Constructor
574.62 -> suppose we're going to redesign the
576.42 -> system to handle this and we create a
579.12 -> default Constructor for proverb manager
581.16 -> if it takes no parameters and doesn't
583.14 -> read from a file this Constructor will
585.3 -> hard code in the proverb objects so just
588.899 -> copy the old Constructor
593.04 -> but create a new arraylist and then add
595.74 -> some Proverbs to it I'm going to pass in
597.6 -> empty strings to save time here but we
599.459 -> can imagine these are constants in the
600.899 -> program for the proverb pieces that will
602.76 -> pass in
614.22 -> now in the catch clause
617.04 -> we can invoke the noar Constructor and
619.2 -> proceed with the game
624.24 -> what's the best option probably failing
627.36 -> and informing the user they need to
628.74 -> reinstall the game or put the file back
630.839 -> if they moved or deleted it but the best
632.88 -> option will depend on the system you're
634.32 -> designing so it's important to
635.82 -> understand what's possible if you're
637.86 -> going to crash and print the stack Trace
639.6 -> because the program can't recover from
641.279 -> the error and you need to do a little
642.899 -> more debugging work to fix it as with
645.54 -> most unchecked exceptions then just let
647.459 -> the jvm handle the error if you're going
649.86 -> to crash but there's something
651.12 -> meaningful you can do like tell the user
653.04 -> to find that file or reinstall then
655.019 -> handle it and take your meaningful
656.339 -> approach if you're not going to crash
658.38 -> but you'll need to take steps before you
660.18 -> can proceed then take the steps in the
662.279 -> exception Handler and proceed with the
664.38 -> program
665.339 -> to practice with this and to learn more
667.44 -> I highly recommend the open source in
669.959 -> other words free textbook Java Java Java
673.44 -> object-oriented problem solving read the
676.079 -> exceptions chapter and practice with the
677.94 -> exercises there I'll include a link to
679.8 -> the book in the description I'll also
681.959 -> include links to some exercises that you
683.82 -> can try to add exception handling to the
685.8 -> proverb scheme the code will also be
688.079 -> available in my GitHub repository
690.42 -> have fun with it

Source: https://www.youtube.com/watch?v=ju1cpFD8NEE