> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/others/throwable/exception/best-practice.md).

# Best Practice

## Scenario 1: A method logic throwing multiple exceptions

Consider the following code where the logic inside it can throw various exceptions.

```java
public static String encrypt(String plainText, String secretKey) throws InvalidAlgorithmParameterException,
            NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException,
            IOException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
   // Some encryption logic             
}
```

Throwing multiple exceptions in a method signature can indicate that the method is handling a wide range of potential issues, which may or may not be a good practice depending on the context.

#### Pros

1. **Explicit Error Handling:** Declaring all potential exceptions makes the method's error handling explicit, allowing the caller to handle each specific case.
2. **Separation of Concerns:** The method handles specific low-level issues (like cryptography errors), keeping the handling separate from the business logic.

#### Cons

1. **Cluttered Signature:** Too many exceptions can make the method signature unwieldy and harder to read.
2. **Poor Abstraction:** Exposing too many implementation details can lead to poor abstraction, making the code harder to maintain and understand.

#### Best practice is to throw a custom exception in this scenario

```java
public class EncryptionException extends Exception {
    public EncryptionException(String message, Throwable cause) {
        super(message, cause);
    }
}

public static String encrypt(String plainText, String secretKey) throws EncryptionException {
    try {
        // encryption logic here
    } catch (InvalidAlgorithmParameterException | NoSuchPaddingException | IllegalBlockSizeException | 
             NoSuchAlgorithmException | IOException | BadPaddingException | 
             NoSuchProviderException | InvalidKeyException e) {
        throw new EncryptionException("Encryption failed", e);
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/others/throwable/exception/best-practice.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
