 Once again, here's a program that asks the user for their age and years and converts it to days. There's a while loop to make sure that the user enters a number that's greater than zero. Let's compile the program and run it. Negative numbers and zero give us error messages and positive numbers give us a correct result. It's still possible to make this program crash by giving non-numeric input. For example, if I put the word 20, the program crashes. In Java, we say that the program throws an exception. And indeed, if you look at the error message, you'll see that we have an input mismatch exception. Exceptions are objects in Java that give information about runtime errors. It's possible to catch these exceptions before they crash your program by using try and catch blocks. You put the code that might throw an exception inside a try block, and then tell what to do when you catch an exception that Java has thrown at you. You catch, give the name of the exception you're trying to catch, which is an input mismatch exception, and a variable in which to store that object. In the catch block, you handle the error. First, we're going to clear out the bad data from the input stream. We'll say input dot next line. We're not assigning that to any variable. We want to take that data and throw it away. And then we want to print an appropriate error message. Please use digits only. One other thing we're going to have to do is we're going to have to import java.util.input mismatch exception. Let's recompile and rerun. Again, negative numbers and zero cause errors, and now non-numeric input also gets caught without crashing our program. And positive numbers still work. Consider this program, which asks for a dividend and divisor, and calculates the quotient and remainder. There are two possible exceptions that can happen in this program. First, we could have non-numeric input, which gives us our input mismatch, or we could try dividing by zero, which would give us an arithmetic exception. Let's use try and catch again. We're going to try block around the code that could be bad. And in our catch block, we will catch exception. That's the parent class for almost all runtime exceptions that we'll ever want to handle. And then we'll print out an error message. We don't need to import anything here. Exception is part of the java.lang package, and it's automatically imported. Let's recompile and rerun it. This time, if we use non-numeric input, it gives us an error, and if we try dividing by zero, we also get an error. While this works, it's not ideal. We'd like to be able to distinguish which exception we had and give an appropriate error message for each different exception. We can do that by adding two different catch clauses. We can catch an input mismatch exception until the users to please use digits only, and we can add another catch clause for arithmetic exception and print out that division by zero is not allowed. We're going to have to import java.util.inputMismatchException, but we don't have to import arithmetic exception because it's part of the java.lang package, and it's automatically imported for us. The variable name that you use for your exception is local to that catch block. That's why I'm able to use the same variable name for all my catch blocks and the compiler won't complain about it. Java checks for exceptions in the order that you specify the catch blocks. When an exception occurs, Java will first check to see if it's an input mismatch. If it's not an input mismatch, it'll check to see if it's an arithmetic exception, and if it's not that, it'll try for the generic exception, which will catch just about everything else. Because exceptions are tested in the order you type them, you always want the most general exception last. Let's compile and let's run. If I put in non-numeric input, I get an appropriate error message. If I try to divide by zero, I get an appropriate error message, and good input works properly. And that's the basics of handling exceptions. The next few videos will go more in-depth about the types of exceptions, how you can throw them, how you should and shouldn't use exceptions.