0
0
Javaprogramming~10 mins

Creating custom exception class in Java - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating custom exception class
Define class extending Exception
Add constructors for message
Throw custom exception in code
Catch custom exception
Handle or print error message
End
First, define a new class that extends Exception, add constructors, then throw and catch it in your code.
Execution Sample
Java
class MyException extends Exception {
  MyException(String message) {
    super(message);
  }
}

public class Main {
  public static void main(String[] args) {
    try {
      throw new MyException("Error happened");
    } catch (MyException e) {
      System.out.println(e.getMessage());
    }
  }
}
Defines a custom exception class and throws it with a message.
Execution Table
StepActionEvaluationResult
1Define class MyException extending ExceptionClass createdMyException class ready
2Create constructor with message parameterConstructor stores messageConstructor ready
3Throw new MyException("Error happened")Exception object createdException thrown
4Catch MyException in try-catch blockCatch block activatedException handled
5Print exception messageMessage retrievedOutput: Error happened
6Program continues or endsNo crashNormal flow resumes
💡 Exception thrown and caught, program continues normally
Variable Tracker
VariableStartAfter ThrowAfter CatchFinal
exceptionObjectnullMyException instance with message "Error happened"Same instance caughtHandled
Key Moments - 3 Insights
Why do we extend Exception to create a custom exception?
Extending Exception lets Java know this is a type of error that can be thrown and caught, as shown in step 1 of the execution_table.
What happens if we throw the custom exception but do not catch it?
If not caught, the program will stop with an error. Step 4 shows catching prevents this and allows normal flow.
How does the message get passed and printed?
The message is passed to the superclass Exception via super(message) in the constructor (step 2), then retrieved when printing in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result at step 3 when the exception is thrown?
AProgram ends immediately
BException caught and handled
CException object created and thrown
DConstructor is called
💡 Hint
Check the 'Result' column in step 3 of execution_table
At which step does the program handle the exception to avoid crashing?
AStep 2
BStep 4
CStep 1
DStep 6
💡 Hint
Look for 'Catch block activated' in the execution_table
If the constructor did not call super(message), what would happen?
AThe exception message would not be stored or printed
BThe program would crash immediately
CThe exception would not be thrown
DThe catch block would not work
💡 Hint
Refer to step 2 and step 5 about message passing and printing
Concept Snapshot
Create a custom exception by:
- Defining a class extending Exception
- Adding constructors calling super(message)
- Throwing with 'throw new MyException("msg")'
- Catching with try-catch
- Accessing message via getMessage()
Full Transcript
To create a custom exception in Java, you define a new class that extends Exception. This class includes a constructor that calls super(message) to store an error message. When you want to signal an error, you throw an instance of this class with a message. In your code, you catch this exception in a try-catch block to handle it gracefully. This prevents the program from crashing and allows you to print or log the error message. The execution steps show defining the class, throwing the exception, catching it, and printing the message, then continuing normal program flow.