Copyright

Exception Types in Java: Checked & Unchecked

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Java has a class for everything, even exceptions (errors). The language has great tools for catching these errors. This lesson will cover the classes for catching errors, describe checked and unchecked exceptions, and provide working code examples.

Exceptions

An exception in Java is an error. It isn't a good thing, but with some extra (and fairly minimal) effort, we can make sure that our code is ready for any unforeseen exceptions. Sometimes Java catches our mistakes before the program is run, but other times the code compiles just fine and the exceptions aren't seen until it's actually running. And, when exceptions happen during run-time, the program will crash.

We'll cover two types of exceptions, and provide a brief overview on how to catch them before they do any damage.

Checked Exceptions

A checked exception is generated when you compile the code. Some developer tools will display an error right in the code, such as a misspelled word in a text editor.


Java exception error in code


Below is the code that is causing the problem. It's trying to open and read a file that doesn't exist:


Java checked exception code


Avoiding Checked Exceptions

There is an approach to catching these exceptions and displaying an error that you define. This can help end users, or other programmers, when running your code. The Java methods are try and catch. Java will attempt to carry out the code in the try section. If it can't, it jumps to the catch method and performs the steps you outline.

Let's look at our previous code, but within a try and catch block. For this lesson we don't need to know each detail of the code below. Focus instead on the try and catch statements and how they work to make sure the checked exceptions are caught.


Java checked exception try and catch


The code that may be troublesome needs to go between the brackets of the try statement. An error reporting or display should go in the catch. It's like a fishing expedition. If you catch a fish, it goes in the bucket. If you don't catch one, you keep on fishing until sundown.

Unchecked Exceptions

While the compiler will alert you to checked exceptions, unchecked exceptions are little nastier. They don't happen until the program runs. Often they are due to bad user input, or calculations within your code that cause problems at run time. The following code will compile, but when it runs, there will be an unchecked exception.


Java unchecked exception


Since the result of the code is division by zero, the following error happens at run-time, stopping the rest of the program from proceeding:


Java divide by zero output


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