Self-made Exception

Try to make an Exception

WahhoiException.java


package exception;

/**
 * <b>Wow exception.<b>
 *
 * @author me
 *
 */
public class WahhoiException extends Exception{

	//Warning avoidance
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor.
	 *
	 * @param msg Output message
	 */
	public WahhoiException (String msg) {
		super(msg);
	}
}

PracticeException1.java


package test;

import java.util.Optional;

import exception.WahhoiException;

/**
 * Test.
 *
 * @author me
 *
 */
public class PracticeException1 {

	/**
	 * main
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			run(Optional.ofNullable(null));
		} catch (WahhoiException e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 *Practice method.
	 *
	 * @param opt I don't know the value of Optional
	 * @throws WahhoiException If the input value is null
	 */
	private static void run(Optional<Object> opt) throws WahhoiException {
		if (opt.isPresent()) {
			System.out.println("Wow!");
		} else {
			throw new WahhoiException("wahhoi");
		}
	}

}

So you can make an original Exception It may not make sense because it already exists, but I made a little!

For example, like this

NoRecordException.java


package exception;

/**
 * <b>Exception that occurs when the number of search results is 0.</b>
 *
 * @author me
 *
 */
public class NoRecordException extends Exception{

	//Warning avoidance
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor.
	 *
	 * @param msg Output message
	 */
	public NoRecordException(String msg) {
		super(msg);
	}
}

Model1.java


package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import exception.NoRecordException;

/**
 * <b>Model1.</b>
 *
 * @author me
 *
 */
public class Model1 {

	/**
	 *Search by primary key.<p>
	 *Search a fictitious table with the primary key,Return the corresponding data in a list.<p>
	 *Be sure to get Exception.
	 *
	 * @param id primary key
	 * @return List of applicable data
	 * @throws NoRecordException When the target record does not exist
	 */
	public List<Model1> find(String id) throws NoRecordException{
		List<Model1> modelList = Collections.synchronizedList(new ArrayList<Model1>());
		// SELECT * FROM table;
		if (!modelList.isEmpty()) {
			return modelList;
		} else {
			throw new NoRecordException("The target record did not exist.");
		}
	}

	/** ID */
	private Integer id;
	/**name*/
	private String name;
	/**Creator name*/
	private String createdBy;
	/**Creation date and time*/
	private Date createdAt;
	/**Updater name*/
	private String updatedBy;
	/**Update date and time*/
	private Date updatedAt;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCreatedBy() {
		return createdBy;
	}
	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}
	public Date getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	public String getUpdatedBy() {
		return updatedBy;
	}
	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}
	public Date getUpdatedAt() {
		return updatedAt;
	}
	public void setUpdatedAt(Date updatedAt) {
		this.updatedAt = updatedAt;
	}

}

Search the data from the table and make an Exception if there are 0 records.

PracticeException2.java


package test;

import java.util.List;

import exception.NoRecordException;
import model.Model1;

/**
 * Test.
 *
 * @author me
 *
 */
public class PracticeException2 {

	/** Model1 */
	private static Model1 model1 = new Model1();

	/**
	 * main
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(run());
	}

	/**
	 *Search the fictitious table,Get the first one of records.<p>
	 *If the corresponding record does not exist,{@code null}Return.<p>
	 *
	 * @return search results
	 */
	private static Model1 run() {

		try {

			//Search the table,Get the first one
			List<Model1> list = model1.find("id");
			return list.stream().findFirst().get();

		} catch (NoRecordException e) {

			//If the corresponding record does not exist
			return null;

		}

	}

}

Well it's commonplace w In this case, it is a findFirst problem, so it has the same meaning as NoSuchElementException. It's an almost meaningless implementation ... w

I'm wondering if Exceptions aren't something you make often, but after all, what kind of cases aren't good? It ’s very important because it informs you well.

I'll forget if I don't use it, so I want to use it more and more

But it seems to be made less often ...

Recommended Posts

Self-made Exception
exception
Exception handling
Exception handling Exception
Drill (self-made)
Java exception handling?
About exception handling
ruby exception handling
Exception switching method
Ruby exception handling
[Java] Exception handling
☾ Java / Exception handling
Java exception handling
Java exception handling