Loading

Custom exception in Java

  • In Java, we can define our own exceptions. You must extend the exception class while declaring the custom exception class.
  • You can create your own message in the custom class.

Example:

package com.quipoin;

public class AgeInvalidException extends Exception{
	public String message;
	public AgeInvalidException(String message) {
		this.message=message;
	}
	public String getMessage() {
		return message;
	}
}
package com.quipoin;

public class Main {
	public static void drivingLicence(int age) throws AgeInvalidException {
		System.out.println("Main method started");
		if(age<18) {
			throw new AgeInvalidException("Not eligible");
		}
		else {
			System.out.println("Eligible for applying driving licence");
		}
	}
	public static void main(String[] args) {
		try {
			drivingLicence(15);
		} catch (AgeInvalidException e) {
			e.getMessage();
			e.printStackTrace();
		}
	}
}

Output:

Main method started
com.quipo.AgeInvalidException: Not eligible
	at com.quipoin.Main.drivingLicence(Main.java:7)
	at com.quipoin.Main.main(Main.java:15)

Throwable class in Java

Key points:

  • The throwable is the base class of all errors and exceptions in the Java language
  • Only the instance of this is thrown by JVM or can be thrown by the throw statement.
  • All error types and exception types inherit Throwable class properties.
  • In other words, the Throwable class defines the properties of an object that can be thrown by JVM or throw statement.
  • Every Throwable type object has a property message which has the details of the Throwable type object. This property might be initialized at the object creation or not.

Methods of Throwable Class

Some of the important methods of Throwable class:

public String getMessage();     
  • Returns the detailed message of the object on which this method is invoked. If the object message property is not set by the constructor it will have null.           
public String getLocalizedMessage();
  • Returns the detailed message of the object on which this method is invoked. A subclass can override this method to provide locale-specific.
public Throwable getCause();
  • Returns the cause of the object on which this method is invoked. If the cause is unknown or non-existent it will return null. The cause is the type of Throwable.
public void printStackTrace();
  • This method returns the stack memory details and the Throwable type object on the output stream. details include exception name, type, and exception line number.
public void printStackTrace(PrintStream s);
  • Print the stack trace details to the specific stream mentioned in the method argument.

Constructors of Throwable class 

  • The Throwable class has overloaded constructors, which helps the constructor the object of throwable type in different ways.

No argument constructor: 

Throwable th=new Throwable();
  • create a Throwable type of object with a null value in the message property.

String argument constructor: 

Throwable th=new Throwable("Learning through Quipo");
  • create a Throwable type of object with the detailed message provided in the constructor.

String and Throwable argument constructor: 

Throwable th=new Throwable("File doesn't exist", c1);
  • create a Throwable type of object with the detail message provided in the first argument and the cause of the object is initialized with a throwable type object in the second argument.

Throwable type argument: 

Throwable th=new Throwable(c1);
  • c1 represents the throwable object that causes the current object.