Java Exception Handling

In Java we have two types of Exceptions, those are checked and unchecked exceptions. Usually checked exceptions can be handled by using the try and catch block in the application code. In the other hand unchecked exceptions occurs during the Runtime of a java application.

What is an Exception?

Exception is nothing but an error or an event occurred during the execution of a Java application, which eventually causes an application to exit or behaves differently during its execution.

Types of Exceptions in Java

There are two types of exceptions in Java

  1. Checked Exceptions
  2. Unchecked Exceptions

Checked Exceptions

Checked Exceptions can be handled via application code using the try and catch block, all these exceptions are derived from the class java.lang.Exception.

Unchecked Exceptions

In the other hand unchecked exceptions cannot be handled 100% via application code but through an extra code can minimize these exceptions. All unchecked exceptions are derived from the java.lang.RuntimeException.

Example : java.lang.NullPointerException

NullPointerException can happen while calling a method or function through a null reference Object,this can be avoided by checking the null reference but this is an extra overhead for the developers

Checked Exception Handling

CopiedCopy Code
public class CheckedExceptionHandling {
	public static void main(String[] args) {
		try {
			// Check whether given password contains specified length or not otherwise throw
			// an exception.
			String password = "abc123";
			if (password.length() >= 8) {
				System.out.println("Password Contains specified length");
			} else {
				throw new Exception("password should contain a minimum length of 8");
			}
		} catch (Exception e) {
			System.out.println("Password Length Exception : "+ e.getMessage());
			e.printStackTrace();
		}
	}
}

When you execute the above program, you'll get the below output.

Output:

Password Length Exception : password should contain a minimum length of 8
java.lang.Exception: password should contain a minimum length of 8
	at CheckedExceptionHandling.main(CheckedExceptionHandling.java:12)

Unchecked Exception Example

CopiedCopy Code
public class UnCheckedException {
	public static void main(String[] args) {
			// Check whether given password contains specified length or not otherwise throw
			// an exception.
			String password = "abc123";
			if (password.length() >= 8) {
				System.out.println("Password Contains specified length");
			} else {
				throw new RuntimeException("password should contain a minimum length of 8");
			}
	}
}

When you execute the above program, you'll get the below output.

Output:

Exception in thread "main" java.lang.RuntimeException: password should contain a minimum length of 8
	at UnCheckedException.main(UnCheckedException.java:11)

User-defined Exception Handling

In the following example, we will handle an exception where a number is not divisible by 0 using custom exceptions

CopiedCopy Code
public class DivisionNumber {
	public static void main(String[] args) throws UserErrorException {
		int number1=10,number2=0;
		if(number2==0) {
			throw new UserErrorException("Can't divide a number by Zero");
		} 
		System.out.println("Division Result :"+number1+"/"+number2+"="+number1/number2);
	}
}
 class UserErrorException extends Exception {
	 UserErrorException(String message) {
		 super(message);
	 }
 }

Output:

Exception in thread "main" UserErrorException: Can't divide a number by Zero
	at DivisionNumber.main(DivisionNumber.java:8)

Explanation:

  1. We declared UserErrorException which extends Exception
  2. Tried pre-checks in the main class method whether any number is zero
  3. If the condition met then throw a user-defined exception