All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Custom Exception In Java Example

Last Updated : Mar 11, 2024

Custom Exception In Java Example

In this article we will show you the solution of custom exception in java example, the Java user-defined exception is a special type of exception that is constructed and is thrown using the keyword "throw."

So By extending the class "Exception," it is quite possible. A problem that occurs while the programme is running is referred to as an exception.

Java's Object-Oriented Programming language produces a potent technique to deal with such exceptions.

Java helps the creation of direct custom exception classes, each of them offers a pretty unique exception class implementation.

User-defined exceptions or custom exceptions are some common names given to these exceptions.

Let's go a bit deeper and learn the syntax and implementation of some user-defined exceptions in Java by solving several examples.

Step By Step Guide On Custom Exception In Java Example :-

An issue (run time error) that happened while a programme was being executed is an exception.

The programme abruptly ends when an exception occurs, and the code after the line that caused the exception is never run.

As derived classes of Exception, Java gives us the ability to define custom exceptions.

A custom exception or user-defined exception is one that we can create on our own. In essence, Java custom exceptions are used to tailor the exception to user requirements.

A User-Defined Exception, often known as a custom exception, is just your own exception class that you create and then toss using the keyword "throw."

Nearly all of the common sorts of programming exceptions are covered by Java exceptions. But occasionally we have to make our own exceptions.

A few justifications for using custom exceptions are as follows:

to detect a subset of current Java exceptions and provide them particular treatment.

Exceptions to business logic These are the business logic and process exceptions. Understanding the precise issue is helpful for both app users and developers.

An exception is a run-time error that takes place whilst a process is running.

When an exception occurs, the programme ends abruptly and the code that follows the line that generated the exception will not run.

The "Exception" class in Java already defines exception classes like ArithmeticException and NullPointerException.

The throw keyword in Java can be used in conjunction with a user-defined exception, though.

The process for writing Java user-defined exceptions is covered in this document.

In Java, we may make our own exception classes and use the throw keyword to raise those exceptions.

These exceptions are referred to as custom or user-defined exceptions. In this article, we'll show you how to build your own custom exception and throw it in response to a certain circumstance.

You should have a basic understanding of Java's throw and try-catch blocks in order to comprehend this tutorial.

class MyException extends Exception {
public MyException(String s)
public class Main {
// Driver Program
public static void main(String args[])
try {
throw new MyException("");
catch (MyException ex) {
  1. Creating class that represents use defined exception
  2. Next a class that uses above MyException
  3. Main method i.e. public static void main(String args[])
  4. Throws an object of user defined exception that will print
  5. Catching the exception and printing caught.
  6. Lastly will print System.out.println(ex.getMessage());
  7. Run the code

Conclusion :-

This is the way we created an example program of user defined exception in Java.

I hope this article on custom exception in java example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪