---
title: "Throwing exceptions and custom exceptions explain with example in Java."  
description: "Throwing exceptions and custom exceptions explain with example in Java."  
author: "ICSM Computer"  
published: 2025-04-24  
updated: 2025-04-24  
canonical: https://www.mindstick.com/interview/34063/throwing-exceptions-and-custom-exceptions-explain-with-example-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Throwing exceptions and custom exceptions explain with example in Java.

## 1. Throwing Exceptions in Java

In Java, you can throw exceptions using the `throw` keyword.

## Syntax:

```java
throw new ExceptionType("Error Message");
```

## Example:

```java
public class ThrowExample {
    public static void checkPersonAge(int age) {
        if (age < 16) {
            throw new IllegalArgumentException("Age must be 16 or above of a person");
        } else {
            System.out.println("Access granted");
        }
    }

    public static void main(String[] args) {
        checkPersonAge(16);  // This will throw an exception
    }
}
```

## 2. Custom Exceptions in Java

You can create your own exceptions by **extending** `Exception` (checked) or `RuntimeException` (unchecked).

## Example of Custom Exception:

```java
// Step 1: Create a custom exception
class InvalidAgeException extends Exception {
    public InvalidAgeException(String message) {
        super(message);
    }
}

// Step 2: Use the custom exception
public class CustomExceptionExample {
    public static void validatePersonAge(int age) throws InvalidAgeException {
        if (age < 16) {
            throw new InvalidAgeException("You must be at least 16 years old for person.");
        }
        System.out.println("Age is valid");
    }

    public static void main(String[] args) {
        try {
            validatePersonAge(15);  // This will throw InvalidAgeException
        } catch (InvalidAgeException e) {
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }
}
```

## Summary:

| Feature | Explanation |
| --- | --- |
| `throw` | Used to explicitly throw an exception |
| `throws` | Declares that a method can throw exceptions |
| Custom Exception | User-defined exception for specific errors |
| `extends Exception` | For checked exceptions |
| `extends RuntimeException` | For unchecked exceptions |

## Answers

### Answer by ICSM Computer

## 1. Throwing Exceptions in Java

In Java, you can throw exceptions using the `throw` keyword.

## Syntax:

```java
throw new ExceptionType("Error Message");
```

## Example:

```java
public class ThrowExample {
    public static void checkPersonAge(int age) {
        if (age < 16) {
            throw new IllegalArgumentException("Age must be 16 or above of a person");
        } else {
            System.out.println("Access granted");
        }
    }

    public static void main(String[] args) {
        checkPersonAge(16);  // This will throw an exception
    }
}
```

## 2. Custom Exceptions in Java

You can create your own exceptions by **extending** `Exception` (checked) or `RuntimeException` (unchecked).

## Example of Custom Exception:

```java
// Step 1: Create a custom exception
class InvalidAgeException extends Exception {
    public InvalidAgeException(String message) {
        super(message);
    }
}

// Step 2: Use the custom exception
public class CustomExceptionExample {
    public static void validatePersonAge(int age) throws InvalidAgeException {
        if (age < 16) {
            throw new InvalidAgeException("You must be at least 16 years old for person.");
        }
        System.out.println("Age is valid");
    }

    public static void main(String[] args) {
        try {
            validatePersonAge(15);  // This will throw InvalidAgeException
        } catch (InvalidAgeException e) {
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }
}
```

## Summary:

| Feature | Explanation |
| --- | --- |
| `throw` | Used to explicitly throw an exception |
| `throws` | Declares that a method can throw exceptions |
| Custom Exception | User-defined exception for specific errors |
| `extends Exception` | For checked exceptions |
| `extends RuntimeException` | For unchecked exceptions |


---

Original Source: https://www.mindstick.com/interview/34063/throwing-exceptions-and-custom-exceptions-explain-with-example-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
