What are checked and unchecked Exceptions in Java?

What are Checked Exceptions?

If you write a piece of code that is logically correct but can fail in certain scenarios then you need to handle those scenarios. So for example if you write code which reads a File and suppose at run time the File is not available then it will throw an Exception. So you can handle this by catching a FileNotFoundException.

What are Runtime Exceptions?

Suppose you write a piece of code which is logically not correct. Say you write code that does not use an API correctly then your code is buggy. So no point handling exception here. Let the error be printed in the log so that you can make corrections to the code.

The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.

What are Errors?

When the exception is beyond your application’s scope. Like some hardware issue. Say at runtime some I/O error occurs.

Both Runtime and Error are unchecked. You don’t need to handle them.

Author: Ankur

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: