Checked Exception In Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

What is a Checked Exception in Java ?

Checked exceptions are the ones that are checked during compilation time. If a code within a method throws a checked exception, then it should either be handled by a method or specify it using the throws keyword.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Types of Checked Exception

  • ClassNotFoundException :
    It is a kind of checked exception that occurs when a Java virtual machine (JVM) tries to load a class but fails because it is unable to find the classpath. Since it is a checked exception, it should be handled explicitly by using the try-catch block or by using the throws keyword.
  • InterruptedException :
    It usually occurs when a thread is interrupted while waiting or sleeping or occupied in some task. During this, a method interrupt() is been called by the code in the thread which blocks the I/O operations.
  • IOException :
    It is an input/output exception and it occurs whenever an input or output operation fails or is interpreted in java. For instance, when you try to read a file that does not exist, it will throw an IOException.
  • SQLException :
    This exception provides detail about SQL database error if it occurs. The error can be SQL syntax or SQL driver.
  • FileNotFoundException :
    It occurs when a file that we are trying to find is unavailable in the directory. It is available in class java.io and is a checked exception because it is thrown by the constructor RandomAccessFile at run time.

Example

This is an example of FileNotFoundException

Java Program to Handle FileNotFound Checked Exception :

Output :

Explanation :
If the given file is not found in the directory (classpath) as mentioned, then it will give an exception FileNotFoundException.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

This is an example of ClassNotFoundException

Code :

Output :

Explanation :
A class newProg is not found in the code hence an exception is thrown.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

How to Handle Checked Exceptions ?

Checked exceptions should be handled properly for the execution of code. There are two ways in which exceptions in java can be handled.

Method - 1 :
Declare the exception using the throws keyword (throws).
Method - 2 :
Handle them using try-catch blocks (try-catch).

Throws :

A keyword throws is used for handling the exceptions. It throws the exception up to the stack to call a method called handle. It is a great way to handle exceptions because it helps code look clean and handle exceptions properly.

Syntax :

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Try-catch Block :

The code is simply wrapped in a try-catch block to handle the exception. It throws the exception within the try-catch block.

Syntax :

When to Use Checked Exception in Java ?

Checked exceptions should be used in case of predictable events and to prevent unpreventable events. The code can be recovered from an exception if it's a checked exception. If the error looks like it can be solved using a checked exception then a checked exception is used. It is mainly used for testing the edge case of the code.

Usually, when software is built, it goes into the testing phase. This is where the checked exceptions come in handy.

Are Checked Exceptions Good Or Bad ?

Well, this is a controversial question and it depends on the programmer's use case. There can be certain cases wherein checked exceptions can be used and some cases where unchecked exceptions are preferred. Whatever the case, exceptions need to be handled properly for preventing the code from being crashed.

Learn More

Checked and unchecked exceptions in java

Conclusion

I hope you liked this blog. Here are a few key takeaways from this blog.

  • Checked exceptions are the ones that are checked during compilation time.
  • Types of Checked Exception :
    • ClassNotFoundException
    • InterruptedException
    • IOException
    • SQLException
    • FileNotFoundException
  • Handle Checked Exceptions :
    • Method - 1 :
      Declare the exception using the throws keyword (throws).
    • Method - 2 :
      Handle them using try-catch blocks (try-catch).
  • Checked exceptions should be used in case of predictable events and to prevent unpreventable events. The code can be recovered from an exception if it's a checked exception.