Handling Exceptions in Java: Try & Catch

Instructor: Doris Wooding

Doris has a master's degree in software engineering.

This lesson will help you learn about try and catch blocks in Java, including information about how to use try and catch blocks for exception handling.

Try/Catch

We've all been there. We try our best at something, but it's doesn't quite work out. Maybe it was something outside our control, or something we failed to anticipate. Then we need to figure out what to do next. But what if you didn't know you had failed?

Let's say you try to register for classes at the university. Maybe a couple of those classes were full, so you didn't get in. You can work with that. You'll pick other classes or try another section.

But let's say you didn't get a message telling you that you weren't able to register for those classes. You thought you had successfully registered for all four classes, but instead you really only registered for two. So you continue on thinking you have your four classes and maybe you don't find out about it until the first day of class, or maybe you don't find out until the end of the semester when those classes don't show up in your history. You need to know something went wrong much sooner than that!

In programming code, it's also important to know when something goes wrong. Java has a great way of handling these situations. In Java, you can try to do something. If the thing you tried doesn't work, you can set up code to deal with that. This is done through try/catch blocks.

Inside the try block, you put the code that you want to run. Then, you make a catch block. If anything goes wrong, the code in the catch block runs. If everything goes well, the code in the catch block is ignored.

Try/Catch Example

Let take a look at how this actually works by digging into some code, starting with a very basic example.

try {
  // TODO: Write the best code ever here
} catch {
  // Oh no! How could the best code ever fail?
}

This example just shows what the try/catch blocks look like. For a real program, they would contain actual code and not just comments. An example of a try/catch block with actual code is shown in the next section.

Exception Handling

When something goes wrong in your code, this is called an error, or an exception. When there is an exception, if the code causing the exception is within a try block, nothing else in the try block will run. Instead the code in the corresponding catch block will run next.

Here is an example with some actual Java code, including basic error handling in the catch block:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support