CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 7 min · free

Exception Handling in Java

7 min4 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Checked and unchecked exceptions, try, catch and finally, the rules for catch order, throw versus throws, and writing your own exception.

Follows the syllabus of: CBSE Class 12 Information Technology (802), ISC Class 12 Computer Science (868), GSEB Std 12 Computer Studies

On screen in this lesson

What is an exception?

An unusual event while the program is running
Java creates an exception object
Not handled? The program stops with a message
Handled? The program recovers and continues

The exception family

ClassParentMeaning
ThrowableObjecttop of the family
ErrorThrowableserious, avoid
ExceptionThrowableproblems we handle
RuntimeExceptionExceptionmistakes in logic

Checked vs unchecked

PointCheckedUnchecked
Checked when?compile timerun time
Must handle?yesoptional
Belongs toExceptionRuntimeException
ExampleIOExceptionArithmeticException

Rules for try blocks

try needs at least one catch or a finally
One try can have many catch blocks
Put child exceptions before parent ones
Only the first matching catch runs
finally runs even after a return

throw vs throws

Pointthrowthrows
What it doesraises onedeclares a risk
Where writteninside a methodmethod heading
Followed byan objectclass names
Examplethrow new ...throws IOException

Common built-in exceptions

ExceptionCaused byType
Arithmetic10 / 0unchecked
ArrayIndexOutOf...m[5], size 5unchecked
NullPointers.length(), s nullunchecked
NumberFormatparseInt("12a")unchecked
IOExceptiona file problemchecked

Quick answers

What is the difference between throw and throws?

throw raises one exception object inside a method; throws declares the risk in the method heading.

Does finally run when the try block returns early?

Yes. finally always runs.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. A user types twelve a instead of twelve, and your whole program crashes. Can Java catch the problem and carry on calmly? Yes, it can. Today we learn checked and unchecked exceptions, try, catch, finally, throw, throws, and how to make our own exception.

First, the new term. An exception is an unusual event that happens while a program is running, like dividing by zero. When it happens, Java creates an exception object that describes the problem. If nobody handles it, the program stops and prints an error message. If we handle it, the program recovers and continues. Exception handling means writing code to deal with these events.

All exceptions belong to one family of classes. Throwable is at the top, and it extends Object. Error is for serious system problems, like running out of memory, which programs should not try to catch. Exception is for problems a program can handle. RuntimeException is a child of Exception, for mistakes in program logic.

Exams love this difference. A checked exception is checked by the compiler, at compile time. So you must handle it, or the program will not even compile. An unchecked exception is a RuntimeException or its child, and handling it is optional. Checked examples are IOException and FileNotFoundException, while unchecked examples are ArithmeticException and NullPointerException. Errors are also counted as unchecked.

Here is a checked exception in action. The file marks dot txt may not exist. So the FileReader constructor can throw FileNotFoundException, which is a checked exception. This code does not compile, and the compiler says the exception must be caught or declared to be thrown. We fix it with try and catch, or with throws, which we learn next.

Put risky code inside a try block. Here we convert the text twelve a into a number with Integer dot parse Int. That fails, so Java throws a NumberFormatException. The rest of the try block is skipped, so n is never printed. Java jumps to the catch block whose type matches. The variable e holds the exception object, and the program prints Not a number.

A finally block runs whether an exception happens or not. It is the right place for clean up work, like closing a file. Ten divided by zero throws an ArithmeticException. The catch block prints e dot get Message, which is slash by zero. Then the finally block prints Done.

Remember five rules. A try block must be followed by at least one catch, or by a finally. One try can have many catch blocks, one for each kind of problem. Write child exceptions like ArithmeticException before a parent like Exception, or the code will not compile. Only the first catch that matches will run. And finally runs even when the try block returns early.

Pause the video and predict the output. The try block first prints A. Then five divided by zero throws an exception, so that print never finishes. The catch for Exception matches, so it prints C. Finally always runs, so it prints D. The answer is A, C, D, on three lines.

These two words look alike, but they do different jobs. The keyword throw raises one exception right now, while throws only warns that a method might raise one. Throw is written inside a method body, and throws is written in the method heading. Throw is followed by one exception object, made with new. Throws is followed by one or more exception class names, separated by commas.

Java does not know the voting age in India is eighteen, but we do. So we raise the exception ourselves with throw. The age is fifteen, which is less than eighteen. We throw a new Exception with the message Too young to vote. The catch block receives it and prints the message.

Now look again at our file program. We added throws IOException to the method heading. That tells Java, this method will not handle the problem, the caller must handle it. FileNotFoundException is a child of IOException, so it is covered. Now the code compiles. Whoever calls open must use try and catch, or add throws too.

Java gives many ready made exceptions, called built in exceptions. The table shortens the names to save space. Arithmetic exception comes from dividing an integer by zero. Array index out of bounds comes from using index five in an array of size five. Null pointer comes from using a variable that holds null, which means no object. Number format comes from converting bad text into a number. IOException is checked, and it comes from input and output problems, like a missing file.

Sometimes no built in exception fits, so we make our own. This is called a user defined exception. Think of an ATM with a low balance. The class LowBal extends Exception, so it is a checked exception. Its constructor passes the message to the parent with super of m. We throw it, catch it, and print No money.

Let us revise. Checked exceptions are checked at compile time and must be handled, while unchecked ones appear at run time. Try holds risky code, catch handles the problem, and finally always runs. Throw raises one exception object. Throws, in the method heading, passes the problem to the caller. And you make your own exception with a class that extends Exception.

Courses that teach this

CourseUnit
CBSE Class 12 Information Technology (802)Fundamentals of Java Programming
ISC Class 12 Computer Science (868)Arrays and Strings
GSEB Std 12 Computer StudiesException Handling and File Handling in Java

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →