kunal-kushwaha/DSA-Bootcamp-Java · info · MyException

name is kunal

Error message

name is kunal

What it means

This is a custom user-defined exception (MyException) thrown explicitly in tutorial code to demonstrate creating and throwing your own exceptions. It fires when a business-rule condition holds (the name equals "Kunal"), mimicking how real libraries signal domain errors. The catch block in main prints e.getMessage(), so the program does not crash.

Source

Thrown at lectures/17-oop/code/src/com/kunal/exceptionHandling/Main.java:12

package com.kunal.exceptionHandling;

public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 0;
        try {
//            divide(a, b);
            // mimicing
            String name = "Kunal";
            if (name.equals("Kunal")) {
                throw new MyException("name is kunal");
            }
        } catch (MyException e) {
            System.out.println(e.getMessage());
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        } catch (Exception e) {
            System.out.println("normal exception");
        } finally {
            System.out.println("this will always execute");
        }

    }

    static int divide(int a, int b) throws ArithmeticException{
        if (b == 0) {
            throw new ArithmeticException("please do no divide by zero");
        }

View on GitHub (pinned to 6bc4d8bf8a)

Solutions

  1. Change the condition or the name value so the throw is not reached, if this is unintended.
  2. If it is intended demo behavior, no fix needed — the try/catch already prints the message.
  3. Replace the demo check with a real domain validation for production use.

Example fix

// before
if (name.equals("Kunal")) {
    throw new MyException("name is kunal");
}
// after
if (name == null || name.isBlank()) {
    throw new MyException("name must not be empty");
}
Defensive patterns

Strategy: try-catch

Validate before calling

// no pre-call validation required; mirror the business rule to avoid the throw:
if (!name.equals("Kunal")) {
    // safe path
}

Try / catch

try {
    // code that may throw MyException
} catch (MyException e) {
    System.out.println(e.getMessage());
}

Prevention

When it happens

Trigger: Running main in com.kunal.exceptionHandling.Main when the local variable name equals "Kunal"; the throw is unconditional for that value: throw new MyException("name is kunal").

Common situations: Learning/teaching custom exception creation; refactoring this demo into real code and forgetting the throw is a stand-in for a domain check; accidentally reusing MyException with its fixed message elsewhere.

Related errors


AI-assisted analysis of kunal-kushwaha/DSA-Bootcamp-Java@6bc4d8bf8a (2026-08-31). Data as JSON: /api/errors/6205e12908599e29. Report an issue: GitHub.