Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java Custom Exception in Java

  • Java Custom Exception refers to the creation of user-defined exception classes in Java. There are times when developers must define their own unique exceptions to handle particular application-specific errors or exceptional conditions, even though Java comes with a set of built-in exceptions to handle common error scenarios.

  • Creating a custom exception involves creating a new class that extends one of the existing exception classes provided by Java, such as Exception or RuntimeException. By defining custom exceptions, developers can provide more meaningful and specific information about the exceptional condition, making it easier to handle and debug errors within their applications.

  • By creating custom exceptions, developers can define their own set of application-specific error conditions and handle them accordingly. This helps in writing more expressive and organized code, making it easier to identify and resolve issues.
Table Of Content

  • Java Custom Exception in Java
  • Example of User Define Exception
  • Second example of User Define Exception






    Why use custom exceptions?


  • A subset of current Java exceptions to be caught and given a particular treatment.

  • Exceptions relating to business logic and workflow are referred to as this. Understanding the precise issue is helpful for both app users and developers.

  • We must extend the Exception class from the java.lang package in order to produce a unique exception.

User Define Exception


class UserDefineException extends Exception {
    public UserDefineException(String s){
     super(s); // calling parent Exception
 }
}

public class Main { // class to use user define above exception
 public static void main(String args[]){
	 try {
	     throw new UserDefineException("DockerTpoint");
	 }
	 catch (UserDefineException ex) {
	     System.out.println("Catch Block");
	     System.out.println(ex.getMessage());
	 }
  }
}





Output:

Catch Block
DockerTpoint 


The call to super is not required when using the constructor of the Exception class, which can also be used without a parameter.

Example : User Define Exception


class UserDefineException extends Exception { }
public class Main { // class to use user define above exception
	public static void main(String args[]){
	    try {
	        throw new UserDefineException(); // Throw a user-defined exception object
	    }
	    catch (UserDefineException ex) {
	        System.out.println("Catch Block");
	        System.out.println(ex.getMessage());
	    }
	}
}



Output:

Catch Block
null 

Java Multithreading Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
custom-exception

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.