Skip to main content

What is Java Custom Exception?

Java Custom Exception


Custom exceptions are exceptions that we can create on our own by inheriting Exception class. This how we can use all the functionalities of the exception class. Java custom exceptions are used to customize the exception according to user need. Here is an example-

  1. package com.Akshay.exceptions;
  2. /**@author Akshay Ingole**/
  3. public class InvalidUserNameException extends Exception {
  4. private static final long serialVersionUID = 1L;
  5. InvalidUserNameException(String s) {
  6. super(s);
  7. }
  8. }

Now your test program is
  1. package com.Akshay.exceptions;
  2. /** @author Akshay Ingole**/
  3. public class TestException {
  4. public static void validateName(String name) throws InvalidUserNameException {
  5. if (" ".equalsIgnoreCase(name) || null == name)
  6. throw new InvalidUserNameException("Name is not valid");
  7. else
  8. System.out.println("Hello " + name);
  9. }
  10. public static void main(String args[]) {
  11. try {
  12. validateName("Akshay");
  13. validateName(" ");
  14. } catch (Exception e) {
  15. System.out.println("Exception occured: " + e);
  16. }
  17. }
  18. }
  19. Output
  20. Hello Akshay
  21. Exception occured: com.Akshay.exceptions.InvalidUserNameException: Name is not valid
Hope this will helps you. 

Comments

Popular posts from this blog

Ace Your Java Interview: Top 50 Java Interview Questions & Answers (2025)

  Introduction Java continues to be a top programming language in 2025 , widely used in enterprise applications, microservices, cloud computing, and AI-based systems . If you're preparing for a Java developer interview , you must be well-versed in Core Java, Java 8-21 features, Spring Boot, Microservices, and Cloud technologies . This guide covers the top 50 Java interview questions for freshers and experienced professionals , ensuring you are ready for your next interview! Table of Contents Core Java Interview Questions Java 8, Java 11, and Java 21 Features OOP and Design Patterns Multithreading and Concurrency JVM, Garbage Collection & Memory Management Collections Framework Spring Boot & Microservices Cloud Technologies (AWS, Azure, GCP) Real-World Java Interview Scenarios 1. Core Java Interview Questions Q1: What are the key features of Java? Answer: Platform Independence – Java runs on JVM , making it OS-independent. Object-Oriented – Java follows...

How to Create Immutable Class and Object in Java

How to create Immutable Class and Object in Java - Tutorial Example Writing or creating immutable classes in Java is becoming popular day by day, because of the concurrency and multithreading advantages provided by immutable objects. Immutable objects offer several benefits over a conventional mutable object, especially while creating concurrent Java applications. An immutable object not only guarantees the safe publication of an object’s state but also can be shared among other threads without any external synchronization. In fact, JDK itself contains several immutable classes like String , Integer , and other wrapper classes. For those who don’t know, immutable objects are those whose state cannot be changed once created. A good example is java.lang.String — once created, a String object cannot be modified. Any operation that seems to modify a String object (like trim() , toUpperCase() , etc.) actually results in a new object. What is an Immutable Class in Java? An immutabl...

How I Cracked 3 Job Offers as a Java Developer – My Journey & Key Strategies

 Job hunting can be overwhelming, especially when you’re aiming for a great opportunity that matches your experience and skills. As a Java developer with nearly six years of experience, I recently went through multiple interview processes and ended up receiving six job offers. It wasn’t luck—it was a mix of strategy, preparation, and the right mindset. In this blog, I’ll share my approach, preparation techniques, and lessons learned to help you navigate your job search and ace your interviews. 1. The Turning Point: Why I Started Looking for a Change Like many professionals, I reached a phase where I wanted career growth, better compensation, and challenging projects. While my experience was strong, I realized that just having skills wasn’t enough—I needed to present them effectively in interviews. 2. Building the Right Mindset for Job Hunting Before diving into job applications, I focused on three key aspects: Confidence: Believing in my experience and technical expertise. Clarity:...