githubEdit

Day 16 - 11 Oct - Exception handling end

Throwing Exceptions

You can throw exceptions manually using the throw keyword. This is useful for creating custom exceptions or re throwing exceptions in your code.

Here is a java code example of throwing a custom exception:

public class CustomExceptionExample {
    public static void main(String[] args) {
        try {
            throw new CustomException("This is a custom exception.");
        }
        catch (CustomException e) {
            System.out.println("Caught custom exception: " + e.getMessage());
        }
    }
}

class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

Using throws Keyword Example

Temporary Example:

Last updated