title
Java Exception Handling Tutorial

description
Learn all about Java Exception Handling and write Java code with confidence! 🔥 Want to master Java? Get my complete Java mastery bundle: http://bit.ly/2tKoy8C 👍 Subscribe for more Java tutorials like this: https://goo.gl/6PYaGF 📕 Get my FREE #Java cheat sheet: http://bit.ly/2JNlQnw ⭐️ Want to learn more from me? Check out these links: Courses: https://codewithmosh.com Twitter: https://twitter.com/moshhamedani Facebook: https://www.facebook.com/programmingwithmosh/ Blog: http://programmingwithmosh.com #Java #Programming

detail
{'title': 'Java Exception Handling Tutorial', 'heatmap': [{'end': 91.519, 'start': 65.86, 'weight': 0.775}, {'end': 363.877, 'start': 327.236, 'weight': 0.806}, {'end': 408.989, 'start': 387.192, 'weight': 0.808}, {'end': 457.359, 'start': 425.271, 'weight': 1}, {'end': 540.422, 'start': 484.177, 'weight': 0.804}, {'end': 968.845, 'start': 950.544, 'weight': 0.79}], 'summary': 'This tutorial covers java exception handling, providing an in-depth understanding of concepts, implementation, troubleshooting, and file handling, emphasizing the importance of handling exceptions to prevent program termination, and discussing the use of finally block for resource cleanup.', 'chapters': [{'end': 146.857, 'segs': [{'end': 27.07, 'src': 'embed', 'start': 1.092, 'weight': 0, 'content': [{'end': 5.335, 'text': "In this video, you're gonna learn everything you need to know about exception handling in Java.", 'start': 1.092, 'duration': 4.243}, {'end': 9.478, 'text': "So by the end of this video, you'll be able to write Java code with confidence.", 'start': 5.755, 'duration': 3.723}, {'end': 17.063, 'text': "Hi, I'm Mash Hamadani and I've taught millions of people how to code through this channel and my online school codewithmash.com.", 'start': 10.018, 'duration': 7.045}, {'end': 20.585, 'text': 'This video is part of my ultimate Java mastery course.', 'start': 17.563, 'duration': 3.022}, {'end': 24.968, 'text': 'So once you finish this video, if you want to learn more, you may want to look at the complete course.', 'start': 20.846, 'duration': 4.122}, {'end': 27.07, 'text': "Now let's jump in and get started.", 'start': 25.549, 'duration': 1.521}], 'summary': 'Learn comprehensive java exception handling for confident coding', 'duration': 25.978, 'max_score': 1.092, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1092.jpg'}, {'end': 92.899, 'src': 'heatmap', 'start': 47.227, 'weight': 1, 'content': [{'end': 50.47, 'text': "As you can see, I've created packages for the other sections of this course.", 'start': 47.227, 'duration': 3.243}, {'end': 55.954, 'text': 'You can find all these packages and all these files in the zip file that I gave you at the beginning of the course.', 'start': 50.71, 'duration': 5.244}, {'end': 62.918, 'text': "Now, in the exceptions package, I'm going to add a new class called exceptions demo.", 'start': 56.855, 'duration': 6.063}, {'end': 65.86, 'text': "this is where I'm going to demonstrate concepts.", 'start': 62.918, 'duration': 2.942}, {'end': 70.382, 'text': "here. I'm going to add a public static void method called show.", 'start': 65.86, 'duration': 4.522}, {'end': 76.186, 'text': 'I made it static so we can call it easily from the main method without having to create an object.', 'start': 70.382, 'duration': 5.804}, {'end': 82.389, 'text': "Now, let's go to the main method and call exceptions demo dot show.", 'start': 76.946, 'duration': 5.443}, {'end': 87.258, 'text': 'Now back to the demo class.', 'start': 85.117, 'duration': 2.141}, {'end': 91.519, 'text': "I'm going to create another static method, public static void.", 'start': 87.258, 'duration': 4.261}, {'end': 92.899, 'text': 'say hello.', 'start': 91.519, 'duration': 1.38}], 'summary': 'Created packages for course sections, added exceptions demo & show method, and created say hello method.', 'duration': 45.672, 'max_score': 47.227, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY47227.jpg'}, {'end': 156.178, 'src': 'embed', 'start': 126.803, 'weight': 2, 'content': [{'end': 132.45, 'text': 'Now, this null pointer exception is a class that is declared in this package, java.lang.', 'start': 126.803, 'duration': 5.647}, {'end': 136.852, 'text': 'So an exception is an object that contains information about an error.', 'start': 133.11, 'duration': 3.742}, {'end': 141.634, 'text': 'In this case, this object is an instance of the null pointer exception class.', 'start': 137.332, 'duration': 4.302}, {'end': 146.857, 'text': "Now we have several exception classes in Java, and you'll learn about them as we go through this section.", 'start': 142.135, 'duration': 4.722}, {'end': 150.119, 'text': "Now let's see where this exception occurs in our program.", 'start': 147.597, 'duration': 2.522}, {'end': 156.178, 'text': 'we got this exception in the say hello method on line 9 of this file.', 'start': 151.175, 'duration': 5.003}], 'summary': 'The null pointer exception occurred in the say hello method on line 9 of the file in the java.lang package.', 'duration': 29.375, 'max_score': 126.803, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY126803.jpg'}], 'start': 1.092, 'title': 'Java exception handling', 'summary': 'Provides an in-depth understanding of exception handling in java, covering the concept, implementation, and potential issues, aimed at enabling the viewer to write java code with confidence as part of the ultimate java mastery course by mash hamadani.', 'chapters': [{'end': 146.857, 'start': 1.092, 'title': 'Java exception handling', 'summary': 'Provides an in-depth understanding of exception handling in java, covering the concept, implementation, and potential issues, aimed at enabling the viewer to write java code with confidence, part of the ultimate java mastery course by mash hamadani.', 'duration': 145.765, 'highlights': ['The chapter covers exception handling in Java, including the concept, implementation, and potential issues, emphasizing the goal of enabling the viewer to write Java code with confidence. Covers the in-depth understanding of exception handling in Java, including concept, implementation, and potential issues, aiming to enable the viewer to write Java code with confidence.', 'Mash Hamadani, an experienced instructor, provides the tutorial as part of the ultimate Java mastery course and emphasizes its relevance to his online school, codewithmash.com. Mash Hamadani, an experienced instructor, provides the tutorial as part of the ultimate Java mastery course and emphasizes its relevance to his online school, codewithmash.com.', 'Demonstrates the creation of packages and classes to illustrate the concepts of exception handling in Java, providing a practical approach to learning. Demonstrates the creation of packages and classes to illustrate the concepts of exception handling in Java, providing a practical approach to learning.', 'Explains the null pointer exception and its significance as a common issue in Java programs, emphasizing the importance of understanding and addressing such exceptions. Explains the null pointer exception and its significance as a common issue in Java programs, emphasizing the importance of understanding and addressing such exceptions.']}], 'duration': 145.765, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1092.jpg', 'highlights': ['The chapter covers exception handling in Java, including the concept, implementation, and potential issues, emphasizing the goal of enabling the viewer to write Java code with confidence.', 'Demonstrates the creation of packages and classes to illustrate the concepts of exception handling in Java, providing a practical approach to learning.', 'Explains the null pointer exception and its significance as a common issue in Java programs, emphasizing the importance of understanding and addressing such exceptions.', 'Mash Hamadani, an experienced instructor, provides the tutorial as part of the ultimate Java mastery course and emphasizes its relevance to his online school, codewithmash.com.']}, {'end': 608.864, 'segs': [{'end': 201.205, 'src': 'embed', 'start': 172.962, 'weight': 0, 'content': [{'end': 176.606, 'text': 'But how did we get to the show method? We got here from the main method.', 'start': 172.962, 'duration': 3.644}, {'end': 183.855, 'text': 'So this information that we have here is called the stack trace, it shows the methods that have been called in the reverse order.', 'start': 177.207, 'duration': 6.648}, {'end': 186.558, 'text': 'And this is very useful when troubleshooting problems.', 'start': 184.035, 'duration': 2.523}, {'end': 189.302, 'text': 'We can see exactly how we got to the offending code.', 'start': 186.819, 'duration': 2.483}, {'end': 192.381, 'text': 'Now, back to our say hello method.', 'start': 190, 'duration': 2.381}, {'end': 197.123, 'text': 'When an exception occurs in a method, we say that method through an exception.', 'start': 193.061, 'duration': 4.062}, {'end': 201.205, 'text': 'Just like how a person can throw a ball, a method can throw an exception.', 'start': 197.443, 'duration': 3.762}], 'summary': 'Stack trace shows method order, useful for troubleshooting.', 'duration': 28.243, 'max_score': 172.962, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY172962.jpg'}, {'end': 288.941, 'src': 'embed', 'start': 259.476, 'weight': 3, 'content': [{'end': 261.339, 'text': 'In Java we have three types of exceptions.', 'start': 259.476, 'duration': 1.863}, {'end': 266.825, 'text': 'Checked exceptions, unchecked exceptions, also called runtime exceptions, and errors.', 'start': 262, 'duration': 4.825}, {'end': 271.79, 'text': 'A lot of people new to Java find this confusing, so let me make it super simple for you.', 'start': 267.385, 'duration': 4.405}, {'end': 277.235, 'text': 'Checked exceptions are exceptions that we developers should anticipate and handle properly.', 'start': 272.43, 'duration': 4.805}, {'end': 280.057, 'text': "For example, let's say we want to read data from a file.", 'start': 277.836, 'duration': 2.221}, {'end': 285.179, 'text': "What if the file doesn't exist? Maybe it got deleted just before we tried to open it for reading.", 'start': 280.557, 'duration': 4.622}, {'end': 288.941, 'text': 'Good developers always anticipate and handle these edge cases.', 'start': 285.68, 'duration': 3.261}], 'summary': 'Java has 3 types of exceptions: checked, unchecked, and errors. checked exceptions should be anticipated and handled properly.', 'duration': 29.465, 'max_score': 259.476, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY259476.jpg'}, {'end': 363.877, 'src': 'heatmap', 'start': 327.236, 'weight': 0.806, 'content': [{'end': 331.997, 'text': "So it's telling us, hey, you haven't handled this exception, file not found exception.", 'start': 327.236, 'duration': 4.761}, {'end': 333.937, 'text': 'This is an example of a checked exception.', 'start': 332.257, 'duration': 1.68}, {'end': 341.259, 'text': "So checked exceptions are exceptions that we should anticipate and recover from, and that is why they're called checked exceptions,", 'start': 334.638, 'duration': 6.621}, {'end': 343.98, 'text': 'because the java compiler checks them at compile time.', 'start': 341.259, 'duration': 2.721}, {'end': 350.706, 'text': 'now the second type of exception is called unchecked exception or runtime exception, as the name implies.', 'start': 344.74, 'duration': 5.966}, {'end': 354.049, 'text': 'these exceptions are not checked by the compiler at compile time.', 'start': 350.706, 'duration': 3.343}, {'end': 356.611, 'text': 'they occur because of programming errors.', 'start': 354.049, 'duration': 2.562}, {'end': 359.233, 'text': 'null pointer exception is an example of a runtime exception.', 'start': 356.611, 'duration': 2.622}, {'end': 361.335, 'text': 'In the last video.', 'start': 360.254, 'duration': 1.081}, {'end': 363.877, 'text': "we shouldn't have passed null to the, say, hello method.", 'start': 361.335, 'duration': 2.542}], 'summary': 'Checked exceptions are anticipated and recovered from, while unchecked exceptions occur due to programming errors like the null pointer exception.', 'duration': 36.641, 'max_score': 327.236, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY327236.jpg'}, {'end': 420.244, 'src': 'heatmap', 'start': 387.192, 'weight': 0.808, 'content': [{'end': 389.234, 'text': "that's a topic for an entirely different course.", 'start': 387.192, 'duration': 2.042}, {'end': 397.08, 'text': 'Now, other examples of runtime exceptions are arithmetic exception, this gets thrown if you try to divide a value by zero.', 'start': 390.094, 'duration': 6.986}, {'end': 403.705, 'text': 'You also have illegal argument exception for indicating that the argument we passed to a method was not accepted.', 'start': 397.6, 'duration': 6.105}, {'end': 408.989, 'text': 'Again, this is a programming mistake, not a kind of error that we should anticipate and validate.', 'start': 404.345, 'duration': 4.644}, {'end': 414.076, 'text': 'Another runtime exception you may see is the index out of bounds exception,', 'start': 409.649, 'duration': 4.427}, {'end': 420.244, 'text': 'which occurs if we try to access an element in an array or a string or a list using an invalid index.', 'start': 414.076, 'duration': 6.168}], 'summary': 'Runtime exceptions include arithmetic, illegal argument, and index out of bounds exceptions.', 'duration': 33.052, 'max_score': 387.192, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY387192.jpg'}, {'end': 457.359, 'src': 'heatmap', 'start': 425.271, 'weight': 1, 'content': [{'end': 431.432, 'text': 'Another popular runtime exception is illegal state exception, which gets thrown if we try to call a method,', 'start': 425.271, 'duration': 6.161}, {'end': 434.093, 'text': 'but the underlying object is not in the right state.', 'start': 431.432, 'duration': 2.661}, {'end': 436.033, 'text': 'So these are runtime exceptions.', 'start': 434.773, 'duration': 1.26}, {'end': 442.434, 'text': 'The third type of exception we have in Java is an error, which indicates an error external to our application.', 'start': 436.593, 'duration': 5.841}, {'end': 446.455, 'text': 'Examples are stack overflow error or out of memory error.', 'start': 442.954, 'duration': 3.501}, {'end': 451.036, 'text': "So if the Java virtual machine runs out of memory, there's nothing we can do about it.", 'start': 447.075, 'duration': 3.961}, {'end': 457.359, 'text': 'So just like runtime exceptions, we should let the application crash rather than display a friendly message to the user.', 'start': 451.616, 'duration': 5.743}], 'summary': "Illegal state exception is a common runtime exception in java, and errors like stack overflow or out of memory can't be handled.", 'duration': 32.088, 'max_score': 425.271, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY425271.jpg'}, {'end': 542.284, 'src': 'heatmap', 'start': 469.925, 'weight': 5, 'content': [{'end': 473.106, 'text': "Next we're going to look at the hierarchy of exception classes in Java.", 'start': 469.925, 'duration': 3.181}, {'end': 483.537, 'text': "Now let's look at the classes that represent different types of exceptions.", 'start': 480.714, 'duration': 2.823}, {'end': 487.58, 'text': 'At the top of the exceptions hierarchy we have the throwable class,', 'start': 484.177, 'duration': 3.403}, {'end': 493.766, 'text': 'which defines the common characteristics for all exceptions and errors like an error message, and the stack trace.', 'start': 487.58, 'duration': 6.186}, {'end': 497.809, 'text': 'So every kind of exception or error has an error message and a stack trace.', 'start': 494.126, 'duration': 3.683}, {'end': 501.832, 'text': 'Below this class we have two classes, exception and error.', 'start': 498.61, 'duration': 3.222}, {'end': 508.657, 'text': 'The error class and all its subtypes represent errors that are external to our application, like out of memory error.', 'start': 502.373, 'duration': 6.284}, {'end': 513.28, 'text': 'The exception class is the parent for all checked and unchecked exceptions.', 'start': 509.337, 'duration': 3.943}, {'end': 519.544, 'text': 'Below this class we have the runtime exception class, which represents runtime or unchecked exceptions.', 'start': 513.88, 'duration': 5.664}, {'end': 525.55, 'text': "So if an exception class derives from the runtime exception, it's considered an unchecked exception.", 'start': 520.125, 'duration': 5.425}, {'end': 527.491, 'text': "Otherwise, it's a checked exception.", 'start': 525.95, 'duration': 1.541}, {'end': 533.536, 'text': 'I know, this is a bit weird, because we may think that any classes that derive from the exception class is a checked exception.', 'start': 527.812, 'duration': 5.724}, {'end': 537.98, 'text': "So runtime exception should also be a checked exception, but that's not how it works.", 'start': 534.137, 'duration': 3.843}, {'end': 540.422, 'text': 'Why? Honestly, I have no clue.', 'start': 538.46, 'duration': 1.962}, {'end': 542.284, 'text': 'You have to ask the developers of Java.', 'start': 540.602, 'duration': 1.682}], 'summary': 'Java exception hierarchy includes throwable, exception, error classes. runtime exceptions are unchecked.', 'duration': 72.359, 'max_score': 469.925, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY469925.jpg'}], 'start': 147.597, 'title': 'Exception handling in java', 'summary': 'Discusses identifying and troubleshooting exceptions, emphasizing the significance of stack trace. it also explains the concept of exception handling in java, including types of exceptions and the importance of handling exceptions to prevent program termination.', 'chapters': [{'end': 192.381, 'start': 147.597, 'title': 'Exception troubleshooting', 'summary': 'Discusses identifying and troubleshooting exceptions in a program, emphasizing the significance of stack trace in understanding the sequence of method calls leading to the error.', 'duration': 44.784, 'highlights': ['The stack trace provides a reverse order of methods called, essential for troubleshooting problems.', 'Identifying the line of exception occurrence (line 9) and the method (say hello) where the exception occurred is crucial for debugging.', 'Understanding the sequence of method calls, such as from show method to say hello method and then to the main method, helps in tracing the cause of the exception.']}, {'end': 608.864, 'start': 193.061, 'title': 'Exception handling in java', 'summary': 'Explains the concept of exception handling in java, including the types of exceptions - checked, unchecked, and errors, and the hierarchy of exception classes, emphasizing the importance of handling exceptions to prevent program termination and the necessity of differentiating between checked and unchecked exceptions.', 'duration': 415.803, 'highlights': ['Java has three types of exceptions: Checked exceptions, unchecked exceptions (runtime exceptions), and errors. Java has three types of exceptions: Checked exceptions, which developers should anticipate and handle properly; unchecked exceptions (runtime exceptions), which occur due to programming errors and are not checked at compile time; and errors, which indicate an error external to the application.', 'Checked exceptions are enforced to be handled at compile time by the Java compiler, while unchecked exceptions occur due to programming errors. Checked exceptions are enforced to be handled at compile time by the Java compiler, while unchecked exceptions occur due to programming errors and are not checked at compile time.', 'The exception hierarchy in Java consists of the Throwable class at the top, followed by the Exception and Error classes, with the RuntimeException class representing unchecked exceptions. The exception hierarchy in Java consists of the Throwable class at the top, followed by the Exception and Error classes, with the RuntimeException class representing unchecked exceptions, and any class deriving from RuntimeException is considered an unchecked exception.']}], 'duration': 461.267, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY147597.jpg', 'highlights': ['Understanding the sequence of method calls helps in tracing the cause of the exception.', 'Identifying the line of exception occurrence and the method where the exception occurred is crucial for debugging.', 'The stack trace provides a reverse order of methods called, essential for troubleshooting problems.', 'Java has three types of exceptions: Checked exceptions, unchecked exceptions (runtime exceptions), and errors.', 'Checked exceptions are enforced to be handled at compile time by the Java compiler, while unchecked exceptions occur due to programming errors.', 'The exception hierarchy in Java consists of the Throwable class at the top, followed by the Exception and Error classes, with the RuntimeException class representing unchecked exceptions.']}, {'end': 842.029, 'segs': [{'end': 638.434, 'src': 'embed', 'start': 609.485, 'weight': 1, 'content': [{'end': 614.786, 'text': "So let's create a file reader object, reader, we set it to a new file reader.", 'start': 609.485, 'duration': 5.301}, {'end': 618.867, 'text': "And here we type file.txt, the name doesn't matter.", 'start': 615.746, 'duration': 3.121}, {'end': 623.768, 'text': 'Now right after this line, we print a message like file open.', 'start': 619.487, 'duration': 4.281}, {'end': 628.809, 'text': "Now we have a compilation error, because we haven't handled the file not found exception.", 'start': 624.648, 'duration': 4.161}, {'end': 633.171, 'text': 'To do this, we should wrap this code inside a try block.', 'start': 629.549, 'duration': 3.622}, {'end': 638.434, 'text': 'so we type try, followed by curly braces to indicate a block of code.', 'start': 633.171, 'duration': 5.263}], 'summary': 'Creating a file reader object and handling file not found exception.', 'duration': 28.949, 'max_score': 609.485, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY609485.jpg'}, {'end': 718.06, 'src': 'embed', 'start': 660.816, 'weight': 2, 'content': [{'end': 664.819, 'text': 'So this x object is an instance of the file not found exception.', 'start': 660.816, 'duration': 4.003}, {'end': 668.22, 'text': "It's an object that contains information about this exception.", 'start': 665.179, 'duration': 3.041}, {'end': 674.546, 'text': 'So, here we add another block, and in this block we print a friendly message to the user.', 'start': 669.261, 'duration': 5.285}, {'end': 678.35, 'text': 'Something like, file does not exist.', 'start': 675.628, 'duration': 2.722}, {'end': 687.9, 'text': "Now, let's run the program, our program didn't crash, this is the proper way to handle checked exceptions.", 'start': 679.331, 'duration': 8.569}, {'end': 692.77, 'text': 'Now we can also get the message from this exception object.', 'start': 688.908, 'duration': 3.862}, {'end': 696.711, 'text': 'so we type x.get message.', 'start': 692.77, 'duration': 3.941}, {'end': 702.193, 'text': 'this returns a string so we can print it on the terminal file.txt.', 'start': 696.711, 'duration': 5.482}, {'end': 704.194, 'text': 'no such file or directory.', 'start': 702.193, 'duration': 2.001}, {'end': 706.435, 'text': 'this error message is defined in the file.', 'start': 704.194, 'duration': 2.241}, {'end': 708.116, 'text': 'not found exception class.', 'start': 706.435, 'duration': 1.681}, {'end': 713.378, 'text': 'okay?. Now, what happened to this statement over here?', 'start': 708.116, 'duration': 5.262}, {'end': 717.52, 'text': 'We tried to print file open, but we are not seeing that message.', 'start': 713.898, 'duration': 3.622}, {'end': 718.06, 'text': "here's the reason.", 'start': 717.52, 'duration': 0.54}], 'summary': 'Properly handle file not found exception, retrieve and print error message.', 'duration': 57.244, 'max_score': 660.816, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY660816.jpg'}, {'end': 772.016, 'src': 'embed', 'start': 747.412, 'weight': 0, 'content': [{'end': 754.293, 'text': "I'm going to take this line out of our try block, put it over here, and then delete the rest of this code.", 'start': 747.412, 'duration': 6.881}, {'end': 761.754, 'text': "So let's say you're creating a file reader object and the compiler is complaining that we haven't handled this checked exception.", 'start': 754.913, 'duration': 6.841}, {'end': 765.575, 'text': "Now, we don't have to manually type a try catch block.", 'start': 762.434, 'duration': 3.141}, {'end': 772.016, 'text': 'We can put the caret over here, press alt and enter, and select surround with try catch.', 'start': 766.015, 'duration': 6.001}], 'summary': 'Code optimization: move line out of try block, use alt+enter to surround with try catch.', 'duration': 24.604, 'max_score': 747.412, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY747412.jpg'}, {'end': 842.029, 'src': 'embed', 'start': 815.798, 'weight': 3, 'content': [{'end': 820.702, 'text': 'here we have a few method calls and this is where the exception was thrown.', 'start': 815.798, 'duration': 4.904}, {'end': 828.289, 'text': 'So sometimes you see a long stack trace because of the method calls in the java standard library or other libraries that you use.', 'start': 821.583, 'duration': 6.706}, {'end': 832.312, 'text': "Next we're going to talk about catching multiple types of exceptions.", 'start': 829.129, 'duration': 3.183}, {'end': 842.029, 'text': 'Sometimes we need to catch multiple types of exceptions.', 'start': 839.628, 'duration': 2.401}], 'summary': 'Discussion on handling exceptions in java method calls and catching multiple types of exceptions.', 'duration': 26.231, 'max_score': 815.798, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY815798.jpg'}], 'start': 609.485, 'title': 'File and exception handling in java', 'summary': 'Covers file handling in java, including creating file reader objects and handling file not found exceptions using try-catch blocks, and also covers catching and handling exceptions in java using try-catch blocks, demonstrating automatic creation of try-catch blocks by intellij, and highlighting the importance of understanding stack traces and catching multiple types of exceptions.', 'chapters': [{'end': 718.06, 'start': 609.485, 'title': 'File handling in java', 'summary': 'Explains how to handle file reading in java, including creating a file reader object, handling file not found exception using try-catch blocks, and retrieving and displaying error messages, ensuring proper handling of checked exceptions.', 'duration': 108.575, 'highlights': ['The chapter explains how to handle file reading in Java, including creating a file reader object, handling file not found exception using try-catch blocks, and retrieving and displaying error messages, ensuring proper handling of checked exceptions. Creating file reader object, handling file not found exception, retrieving and displaying error messages', "The program didn't crash, demonstrating the proper way to handle checked exceptions. Program didn't crash", "Demonstrates the use of x.getMessage() to retrieve and print the error message 'file.txt: no such file or directory' from the exception object x. Use of x.getMessage() to retrieve and print error message"]}, {'end': 842.029, 'start': 719.414, 'title': 'Java exception handling', 'summary': 'Explains how to catch and handle exceptions in java using try-catch blocks, demonstrates the automatic creation of try-catch blocks by intellij, and highlights the importance of understanding stack traces and catching multiple types of exceptions.', 'duration': 122.615, 'highlights': ['IntelliJ automatically creates a try-catch block for handling exceptions IntelliJ can automatically generate a try-catch block for handling exceptions, simplifying the process for the developer.', 'Understanding stack traces and their significance in debugging The significance of understanding stack traces, which provide information about the origin and sequence of method calls and can aid in debugging code.', 'Demonstration of catching multiple types of exceptions The importance of catching multiple types of exceptions in Java, providing a comprehensive approach to handling various potential error scenarios.']}], 'duration': 232.544, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY609485.jpg', 'highlights': ['IntelliJ can automatically generate a try-catch block for handling exceptions, simplifying the process for the developer.', 'The chapter explains how to handle file reading in Java, including creating a file reader object, handling file not found exception using try-catch blocks, and retrieving and displaying error messages, ensuring proper handling of checked exceptions.', "The program didn't crash, demonstrating the proper way to handle checked exceptions.", 'Understanding stack traces, which provide information about the origin and sequence of method calls and can aid in debugging code.', 'Demonstration of catching multiple types of exceptions in Java, providing a comprehensive approach to handling various potential error scenarios.', "Demonstrates the use of x.getMessage() to retrieve and print the error message 'file.txt: no such file or directory' from the exception object x."]}, {'end': 1140.688, 'segs': [{'end': 934.793, 'src': 'embed', 'start': 911.616, 'weight': 0, 'content': [{'end': 919.458, 'text': 'So this second catch block will get ignored, okay? So we have multiple catch blocks, and each catch block targets a specific type of exception.', 'start': 911.616, 'duration': 7.842}, {'end': 923.865, 'text': 'Now, does the order of these catch blocks matter? Sometimes it does.', 'start': 920.138, 'duration': 3.727}, {'end': 929.509, 'text': 'So if I move this catch block over here, you see a compilation error.', 'start': 924.585, 'duration': 4.924}, {'end': 934.793, 'text': 'The java compiler is saying that we have already caught the find not found exception.', 'start': 930.129, 'duration': 4.664}], 'summary': 'Multiple catch blocks target specific exceptions, with order affecting compilation errors.', 'duration': 23.177, 'max_score': 911.616, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY911616.jpg'}, {'end': 990.139, 'src': 'heatmap', 'start': 950.544, 'weight': 2, 'content': [{'end': 959.802, 'text': "this class extends the ioException class, and that means, if you have a cache class for an ioException, we don't need this.", 'start': 950.544, 'duration': 9.258}, {'end': 960.904, 'text': 'findNotFoundException.', 'start': 959.802, 'duration': 1.102}, {'end': 965.383, 'text': 'This is the polymorphism principle of object oriented programming.', 'start': 962.301, 'duration': 3.082}, {'end': 967.765, 'text': 'We talked about this in the second part of this course.', 'start': 965.463, 'duration': 2.302}, {'end': 968.845, 'text': 'Let me show you.', 'start': 968.365, 'duration': 0.48}, {'end': 980.373, 'text': "If I have a variable of type IOException let's call it x I can set this variable to a new instance of IOException or any classes that extend the IOException class.", 'start': 969.466, 'duration': 10.907}, {'end': 984.755, 'text': 'So we can set this to a new file not found exception.', 'start': 980.953, 'duration': 3.802}, {'end': 987.377, 'text': 'This is polymorphism.', 'start': 986.236, 'duration': 1.141}, {'end': 990.139, 'text': 'An object may take different forms.', 'start': 987.777, 'duration': 2.362}], 'summary': 'Polymorphism in object-oriented programming allows an ioexception variable to be set to a new instance of ioexception or any extending class, such as filenotfoundexception.', 'duration': 24.676, 'max_score': 950.544, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY950544.jpg'}, {'end': 1119.99, 'src': 'embed', 'start': 1091.894, 'weight': 1, 'content': [{'end': 1094.317, 'text': 'So these are different ways to catch multiple exceptions.', 'start': 1091.894, 'duration': 2.423}, {'end': 1098.044, 'text': 'The approach you take really depends on your application and its requirements.', 'start': 1094.698, 'duration': 3.346}, {'end': 1100.708, 'text': "Next we're going to talk about the finally block.", 'start': 1098.685, 'duration': 2.023}, {'end': 1115.767, 'text': "Let's imagine this file exists, so we successfully open it for reading, but something goes wrong when we try to read data from this file.", 'start': 1108.581, 'duration': 7.186}, {'end': 1118.169, 'text': 'This could happen because of a hardware problem.', 'start': 1116.327, 'duration': 1.842}, {'end': 1119.99, 'text': 'Now we have a problem in this code.', 'start': 1118.729, 'duration': 1.261}], 'summary': 'Ways to catch multiple exceptions, application-dependent approach, and use of finally block in dealing with file reading errors.', 'duration': 28.096, 'max_score': 1091.894, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1091894.jpg'}], 'start': 842.59, 'title': 'Java and oop exception handling', 'summary': 'Demonstrates java exception handling, including io and file not found exceptions, with examples of catch clauses and their order in the code. it also discusses polymorphism in object-oriented programming and how to handle exceptions by catching various derivatives of the ioexception class, emphasizing the importance of the finally block for releasing file handles.', 'chapters': [{'end': 934.793, 'start': 842.59, 'title': 'Java exception handling', 'summary': 'Demonstrates java exception handling, including handling io and file not found exceptions, with examples of using catch clauses and the importance of their order in the code.', 'duration': 92.203, 'highlights': ['The read method may throw an io exception if it cannot read data from the file, requiring a catch clause to handle it.', 'Multiple catch blocks target specific types of exceptions, with only one being executed, as demonstrated when trying to open a non-existent file.', 'The order of catch blocks can matter, as rearranging them can result in compilation errors, as seen when attempting to move a catch block for file not found exception.']}, {'end': 1140.688, 'start': 934.973, 'title': 'Exception handling in object-oriented programming', 'summary': 'Discusses the principles of polymorphism in object-oriented programming, demonstrating how to handle exceptions by catching various derivatives of the ioexception class and the importance of the finally block for releasing file handles.', 'duration': 205.715, 'highlights': ['Principles of Polymorphism Explains the polymorphism principle in object-oriented programming where a variable of type IOException can be set to a new instance of IOException or any classes that extend the IOException class, illustrating the concept of an object taking different forms.', 'Handling Exceptions Demonstrates the process of catching multiple exceptions by combining catch clauses using a vertical bar, showcasing the flexibility in treating exceptions differently based on specific application requirements.', 'Importance of Finally Block Emphasizes the significance of the finally block in releasing file handles as failure to do so may lead to inaccessible resources for other processes, highlighting the necessity of closing file handles as an essential practice when working with file resources.']}], 'duration': 298.098, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY842590.jpg', 'highlights': ['The order of catch blocks can matter, as rearranging them can result in compilation errors, as seen when attempting to move a catch block for file not found exception.', 'Handling Exceptions Demonstrates the process of catching multiple exceptions by combining catch clauses using a vertical bar, showcasing the flexibility in treating exceptions differently based on specific application requirements.', 'Principles of Polymorphism Explains the polymorphism principle in object-oriented programming where a variable of type IOException can be set to a new instance of IOException or any classes that extend the IOException class, illustrating the concept of an object taking different forms.']}, {'end': 1376.031, 'segs': [{'end': 1209.363, 'src': 'embed', 'start': 1159.758, 'weight': 0, 'content': [{'end': 1166.901, 'text': "Now, the reader variable is not recognized because we have declared it inside this try block, so it's only accessible in this block.", 'start': 1159.758, 'duration': 7.143}, {'end': 1169.342, 'text': "But let's not worry about it for a second.", 'start': 1167.381, 'duration': 1.961}, {'end': 1179.306, 'text': 'the problem with this implementation is that sometime in the future another developer might come here and write some extra code and this code may throw an exception.', 'start': 1169.342, 'duration': 9.964}, {'end': 1182.688, 'text': 'Again, our reader is not going to get closed.', 'start': 1179.927, 'duration': 2.761}, {'end': 1187.23, 'text': 'The proper way to handle this kind of scenario is to use the finally block.', 'start': 1183.408, 'duration': 3.822}, {'end': 1197.776, 'text': 'So, right after the catch block we type finally now in this block we can release external resources like file handles, database connections,', 'start': 1187.91, 'duration': 9.866}, {'end': 1198.937, 'text': 'network connections and so on.', 'start': 1197.776, 'duration': 1.161}, {'end': 1201.578, 'text': 'So here we should type reader.', 'start': 1199.777, 'duration': 1.801}, {'end': 1209.363, 'text': "close, but once again, reader is not recognized because it's only accessible in the try block.", 'start': 1202.879, 'duration': 6.484}], 'summary': 'Use a finally block to release external resources, like file handles or database connections, ensuring proper cleanup and avoiding exceptions.', 'duration': 49.605, 'max_score': 1159.758, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1159758.jpg'}, {'end': 1266.93, 'src': 'embed', 'start': 1237.276, 'weight': 5, 'content': [{'end': 1239.958, 'text': "So the compiler doesn't know what is the type of this variable.", 'start': 1237.276, 'duration': 2.682}, {'end': 1244.052, 'text': "So, let's type file reader.", 'start': 1239.978, 'duration': 4.074}, {'end': 1249.857, 'text': 'Now, we have a compilation error, variable reader might not have been initialized.', 'start': 1245.133, 'duration': 4.724}, {'end': 1254.341, 'text': 'Because in Java, before we can access an object, we should always initialize it.', 'start': 1250.397, 'duration': 3.944}, {'end': 1262.027, 'text': "So if this line doesn't get executed, if it throws an exception, we'll try to use an uninitialized object.", 'start': 1255.121, 'duration': 6.906}, {'end': 1266.93, 'text': 'To solve this problem, we need to explicitly set this to null.', 'start': 1262.627, 'duration': 4.303}], 'summary': 'Compiler error: variable might not have been initialized, solution: set to null', 'duration': 29.654, 'max_score': 1237.276, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1237276.jpg'}, {'end': 1340.598, 'src': 'embed', 'start': 1314.269, 'weight': 1, 'content': [{'end': 1320.793, 'text': 'Now, that aside, what I want you to take away here is that this find in the block will always get executed.', 'start': 1314.269, 'duration': 6.524}, {'end': 1322.594, 'text': 'Whether we have an exception or not.', 'start': 1321.233, 'duration': 1.361}, {'end': 1331.3, 'text': "If we don't have an exception, all the code inside the try block will get executed, and then the runtime will pass control to the find in the block.", 'start': 1323.415, 'duration': 7.885}, {'end': 1340.598, 'text': 'If one of these lines throws an exception, one of our catch blocks will get executed, and then the finally block will get executed.', 'start': 1332.395, 'duration': 8.203}], 'summary': 'The finally block always gets executed, with or without exceptions.', 'duration': 26.329, 'max_score': 1314.269, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1314269.jpg'}], 'start': 1141.729, 'title': 'Proper use of finally block and java exception handling', 'summary': 'Discusses the importance of using the finally block for resource cleanup in exception handling, emphasizing proper resource management and also highlights the need for initializing variables and utilizing try-catch blocks in java exception handling with a comparison to c sharp.', 'chapters': [{'end': 1209.363, 'start': 1141.729, 'title': 'Proper use of finally block', 'summary': 'Discusses the problem of resource cleanup in exception handling, emphasizing the importance of using the finally block to release external resources like file handles, database connections, and network connections to ensure proper resource management and avoid potential issues caused by unhandled exceptions.', 'duration': 67.634, 'highlights': ['The finally block is crucial for releasing external resources like file handles, database connections, and network connections to ensure proper resource management and avoid potential issues caused by unhandled exceptions.', 'Exceptions thrown in the try block may prevent subsequent lines of code from executing, necessitating the use of the finally block for resource cleanup.', "The variable 'reader' declared inside the try block is not accessible outside its scope, highlighting the importance of resource management in the context of exception handling."]}, {'end': 1376.031, 'start': 1209.903, 'title': 'Java exception handling', 'summary': 'Discusses java exception handling, emphasizing the need to initialize variables, utilizing try-catch blocks, and ensuring the execution of the finally block, while also highlighting the availability of a better exception handling structure in c sharp.', 'duration': 166.128, 'highlights': ['The finally block will always get executed, emphasizing the importance of its execution regardless of exceptions.', 'The need to explicitly initialize variables to avoid compilation errors and potential use of uninitialized objects.', 'Utilizing try-catch blocks to handle exceptions and ensuring proper resource release.']}], 'duration': 234.302, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/_nmm0nZqIIY/pics/_nmm0nZqIIY1141729.jpg', 'highlights': ['The finally block is crucial for releasing external resources like file handles, database connections, and network connections to ensure proper resource management and avoid potential issues caused by unhandled exceptions.', 'The finally block will always get executed, emphasizing the importance of its execution regardless of exceptions.', 'Exceptions thrown in the try block may prevent subsequent lines of code from executing, necessitating the use of the finally block for resource cleanup.', 'Utilizing try-catch blocks to handle exceptions and ensuring proper resource release.', "The variable 'reader' declared inside the try block is not accessible outside its scope, highlighting the importance of resource management in the context of exception handling.", 'The need to explicitly initialize variables to avoid compilation errors and potential use of uninitialized objects.']}], 'highlights': ['The finally block is crucial for releasing external resources like file handles, database connections, and network connections to ensure proper resource management and avoid potential issues caused by unhandled exceptions.', 'Understanding the sequence of method calls helps in tracing the cause of the exception.', 'IntelliJ can automatically generate a try-catch block for handling exceptions, simplifying the process for the developer.', 'The chapter covers exception handling in Java, including the concept, implementation, and potential issues, emphasizing the goal of enabling the viewer to write Java code with confidence.', 'The chapter explains how to handle file reading in Java, including creating a file reader object, handling file not found exception using try-catch blocks, and retrieving and displaying error messages, ensuring proper handling of checked exceptions.']}