Custom exceptions in Java

Previous
Next

Custom Exceptions:

  • Java API providing pre-defined exception classes.
  • Every programmer can define user exceptions also.
  • A custom exception extends the functionality from pre-defined exception classes.
  • Custom exception can be either Checked or Unchecked.
  • Most of the custom exceptions are Checked.
class CustomException extends RuntimeException
{
	// user defined unchecked exception
}

class CustomException extends Exception
{
	// user defined checked exception
}

How we construct exception object with Error-information?

  • Every exception class has pre-defined constructor with String argument.
  • We need to call Parent class constructor by specifying the error name.
  • Using pre-defined functionality, pre defined exception class creates object.
/*class RuntimeException
{
	public RuntimeException(String message)
	{
		Constructs a new runtime exception with the specified detail message. 
	}
}*/
class CustomException extends RuntimeException
{
	CustomException(String name)
	{
		super(name);
	}
}
class CreateException 
{
	public static void main(String args[])
	{
		new CustomException("Error-Message");
	}
}
Previous
Next

Add Comment

Courses Enquiry Form