[JAVA] Create exceptions with a fluid interface

The exception interface that you try to do with a constructor with arguments is awkward to use, isn't it? That's the story.


Problematic exception

For example, if you want to make an exception with two unique fields

public class SampleException extends RuntimeException {

    private String code;

    private String id;


Constructor We will talk about what to prepare.

It's hard to start making a constructor thinking about the combination of these. Furthermore, since code, ʻid, and message` are of the same type, it is not possible to create a constructor that sets only that for each.

    public SampleException(String message, String id, String code) {
        this.id = id;
        this.message = message;

You can put Null where it doesn't fit. You might think, but it's not smart. Also, as the number of constructors increases, the user has to worry about the order, which is difficult. (If you write javadoc properly, you can understand it, but if you don't look at it one by one, you can't understand.) It's this level with two fields, so it would be disastrous if there were more fields ...

Solved with a flowing interface

public class SampleException extends RuntimeException {

    private String code;

    private String id;

    private SampleException(String message, Throwable cause) {
        super(message, cause);

    public static class SampleExceptionThrower {

        private String message;

        private Throwable cause;

        private String code;

        private String id;

        private SampleExceptionThrower() {

        public static void call(Consumer<SampleExceptionThrower> consumer) {
            SampleExceptionThrower builder = new SampleExceptionThrower();
            SampleException exception = new SampleException(builder.message, builder.cause);
            exception.code = builder.code;
            exception.id = builder.id;
            throw exception;

        public SampleExceptionThrower setMessage(String message) {
            this.message = message;
            return this;

        public SampleExceptionThrower setCause(Throwable cause) {
            this.cause = cause;
            return this;

        public SampleExceptionThrower setId(String id) {
            this.id = id;
            return this;

        public SampleExceptionThrower setCode(String code) {
            this.code = code;
            return this;



Fluent Builder pattern. You can use it like this.

SampleExceptionThrower.call(builder -> builder.setCause(e)

Very clean!


I don't like the name call, so please give me some good ideas ...

Recommended Posts

Create exceptions with a fluid interface
Exception handling with a fluid interface
Create a playground with Xcode 12
Create a Vue3 environment with Docker!
Have fun programming with lambda expressions and a fluid interface
Create a Maven project with a command
Create a jar file with the command
[Rails6] Create a new app with Rails [Beginner]
Create a simple web application with Dropwizard
[Rails withdrawal] Create a simple withdrawal function with rails
Create a MySQL environment with Docker from 0-> 1
[Rails 5] Create a new app with Rails [Beginner]
[Memo] Create a CentOS 8 environment easily with Docker
Create a simple search app with Spring Boot
Create a CSR with extended information in Java
Create a simple bulletin board with Java + MySQL
[Rails] rails new to create a database with PostgreSQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Let's create a timed process with Java Timer! !!
[Java] Create a collection with only one element
Create a team chat with Rails Action Cable
Create a SandBox account with fastlane spaces ip
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Try create with Trailblazer
[Java] Create a filter
Create an EC site with Rails 5 ⑨ ~ Create a cart function ~
Create a widget template for iOS14 with Intent Configuration.
Create a Chat app with WebSocket (Tyrus) + libGDX + Kotlin
Create a user with an empty password on CentOS7
[Note] Create a java environment from scratch with docker
Create a Service with an empty model Liferay 7.0 / DXP
Create a simple demo site with Spring Security with Spring Boot 2.1
Create a blog with Jekyll and GitHub Pages @ Theme setting
I tried to create a java8 development environment with Chocolatey
Tutorial to create a blog with Rails for beginners Part 1
Create a page control that can be used with RecyclerView
Create a Docker image with the Oracle JDK installed (yum
[Rails] I tried to create a mini app with FullCalendar
Create a Hello World web app with Spring framework + Jetty
Create a blog with Jekyll and GitHub Pages @ Initial Settings
Create a SlackBot with AWS lambda & API Gateway in Java
A story addicted to toString () of Interface proxied with JdkDynamicAopProxy
Until you create a Web application with Servlet / JSP (Part 1)
Tutorial to create a blog with Rails for beginners Part 2
I tried to create a padrino development environment with Docker
Tutorial to create a blog with Rails for beginners Part 0
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Create XML-RPC API with Wicket
Create a java method [Memo] [java11]
Create a VS Code Plugin.
Draw a gradient with CAGradientLayer
Create a fortune using Ruby
Create microservices with Spring Boot
How to create a method
A story stuck with NotSerializableException
Create a name input function
[SAP] Create a development environment with NW AS ABAP Developer Edition (1)
Create a simple web server with the Java standard library com.sun.net.httpserver
I made a command line interface with WinMerge Plugin using JD-Core
Create a simple CRUD with SpringBoot + JPA + Thymeleaf ④ ~ Customize error messages ~