Java Exceptions — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 46 / 50 · தமிழ்

Java Exceptions

Handle a failure where you can do something useful.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

  • Explain the key idea behind Exceptions – Try…Catch.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

An exception transfers control when an operation cannot complete normally. Catch a specific type when you can recover, add context, or choose a fallback. Avoid empty catch blocks that hide failures and make debugging harder.

02 / Step by step

விரிவான விளக்கங்கள்

Java Exceptions: பிழைகளை எப்படி கையாள்வது?

Exception என்பது program run ஆகும்போது occur ஆகுற unexpected event — program crash ஆகாம handle பண்றதுக்கு try-catch use பண்றோம்.

Try-Catch

try block-ல risky code போடறோம். Error வந்தா catch block execute ஆகும்:

Finally Block

finally block எப்பவும் execute ஆகும் — exception வந்தாலும் இல்லன்னாலும்:

Throw மற்றும் Throws

throw keyword-ல manually exception throw பண்ணலாம்:

Common exceptions: ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException.

Exception Hierarchy

ExceptionCause
IOExceptionFile read/write failure
NullPointerExceptionAccessing a null object
ArrayIndexOutOfBoundsExceptionArray index out of range
NumberFormatExceptionCannot parse string to number
ArithmeticExceptionMath error (e.g., divide by zero)

throw vs throws

Java reference fragmentJAVA
// throw — manually raise an exception
void checkAge(int age) {
    if (age < 18) throw new IllegalArgumentException("Too young!");
}

// throws — declare that a method may throw
void readFile(String path) throws IOException {
    // risky file operation
}

Best practice: Catch specific exceptions (not just Exception), always close resources in finally or use try-with-resources, and never swallow exceptions silently.

Broad Exception-ஐ catch செய்யாதீர்கள்: catch (Exception e) என்று எழுதுவது anti-pattern. இது hidden bugs-ஐ suppress செய்யும். எப்போதும் specific exceptions மட்டும் catch செய்யவும் — catch (IOException e), catch (NumberFormatException e) போன்று. Production code review-ல் இது மிக முக்கியமான point.

03 / Read and predict

முழுமையான உதாரணம்

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        try {
            int count = Integer.parseInt("not-a-number");
            System.out.println(count);
        } catch (NumberFormatException ex) {
            System.out.println("Please enter a whole number.");
        }
        System.out.println("Program continues.");
    }
}
Predict first. Then reveal the expected output / வெளியீடு
Please enter a whole number.
Program continues.

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Replace the input with 12 and predict whether the catch block runs.

Reveal the answer and reasoning / விடை

The catch block does not run. The program prints 12 and then Program continues.

தவிர்க்க வேண்டிய தவறு: Do not catch Exception indiscriminately when a specific exception describes the failure you intend to handle.

பாடத்தின் சுருக்கம்

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.