Check out a free preview of the full Java Fundamentals course

The "Handling Exceptions" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses handling events that disrupt the normal flow of a program and walks through creating a program with exception handling. Printing out the error message using the getMessage method is also demonstrated in this segment.

Preview
Close

Transcript from the "Handling Exceptions" Lesson

[00:00:00]
>> So this next chapter is on exceptions, and an exception is an event that occurs due to an error. So we've run a few programs and we try some really wild things and I told you all that's going to crash. Those crashes are called exceptions. They disrupt the normal flow of the program, everything halts, and you get an error, okay?

[00:00:27]
With exceptions like this one, this code compiles, right? There are no errors, there are no warnings. However, if we were to run this, we would get an error when the loop attempts to access index three of this array because there is no index three, right? I have 0, 1, and 2 in this array.

[00:00:53]
This is saying while i is less than 4, so when it tries to do when i is equal to 3, an exception will be thrown, and the specific exception would be an ArrayIndexOutOfBoundsException, all right? The program would crash, it would not continue running after that. So it halts the execution of the program.

[00:01:21]
So we're going to create a new program where we're gonna handle some exceptions within our code versus letting them just interrupt the flow of the program. And in this program, we're gonna create a new file. So this will also give us a little dabbling into working with external resources, and we're gonna handle any of the exceptions that it may throw, okay?

[00:01:51]
So in IntelliJ, [LAUGH] we're going to create a new package, and we'll call this exemptions. Within exception, let's create a new class, and we'll just call it ExceptionHandling. Let's create our main. I'm just gonna do everything in here if you don't mind, all right? Okay, so we got enough going on.

[00:02:27]
[LAUGH] So we're gonna create a, well, we're gonna try to create a new file. In order to make a file, we are gonna use the file class as provided to us by Java in the java.io package. We'll name this file, and we'll say new File. So this is essentially creating a file object, not an actual file on the file system.

[00:02:56]
That makes sense. But you can point it to the path name where the file exists on the system. Let's give it something that does not exist, because that's the kinda stuff that throws exceptions, okay? So we're gonna say, all right, what about look inside of this resources folder for a file called nonexistent.txt.

[00:03:27]
All right, now, and if we wanted to create this, it's okay that this file doesn't exist, that's fine. We could use the file object to actually create a new file. So there's this method called createNewFile. And notice we have an error right here. What this error says is that there's an unhandled exception called a java.io.IOException.

[00:03:57]
So it is already anticipating that, hey, this method could possibly throw an exception. If you're gonna call this method, you got to handle the exception in your code. So there's a couple of ways that you can handle it. One way is by using a try-catch block. So you try something that has the potential of throwing an exception.

[00:04:21]
If that exception is thrown, you use a catch block to catch it and kinda basically mitigate or at least end the program gracefully without just an exception being thrown. So how we'll do this is by writing the word try and a set of curly braces, and then anything that you want to try that might possibly throw an exception, you will add it inside of the try block, okay?

[00:04:51]
After try is where we would write the catch block. When you write catch, you need a set of parentheses and inside of here, you specify which exception that you are going to catch. So this was throwing an IOException. That's the exception that I'd like to catch. You give it a name.

[00:05:13]
So we'll just call it e by convention, and notice that error has now gone away because I provided a framework for catching the exception should it be thrown. All right, so when I catch this exception, for now I just want to exit gracefully. So I'm gonna just print out something like, sorry, an error occurred.

[00:05:46]
And then that's it, right? So let's just run this much. Okay, now, this exception was thrown, not because nonexistent.txt doesn't exist, cuz we said create a new file. But because resources to the directory doesn't exist, so it can't create that new file with the directory not even being there.

[00:06:15]
So I don't know that, right? I just said, sorry, an error occurred. There are some methods available on the exception that will provide more detail. So for example, instead of saying, sorry, an error occurred, I could say, actually print the message. So maybe I'll do that, and then say e.getMessage.

[00:06:42]
This will give me the error message that was returned with the exception. So boom, now I have much more context. It says, sorry, an error occurred, and now it's specifying no such file or directory.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now