---
# System prepended metadata

title: 錯誤訊息實作檔案

---

# 錯誤訊息實作檔案
```java=
public class MyException extends RuntimeException { //運行時才產生Exception
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L; //錯誤訊息需要版本號

	public MyException() { //做一個空建構子可供使用

	}

	public MyException(String message) {
		super(message);//直接用父類別有的
	}

}

```
```java=
public class Demo04 {

	private int id;

	public void test01() {

	}

	public void model(int id) {
		if (id > 0) {
			this.id = id; //賦值
		} else {
			throw new MyException("輸入錯誤");
		}
	}
}

```
```java=
//進階用法
        Employee emp = new Employee();
        // 輸入檢查
        String name = input_eName.getText();
        String age = input_eAge.getText();
        String mail = input_eEmail.getText();

        if (name.length() > 0 && name.length() < 25) {
            emp.seteName(input_eName.getText());
        } else {
            throw new MyException("姓名欄位不得為空白");
        }

        if (age.length() > 0 && age.length() < 25) {
            emp.seteAge(input_eAge.getText());
        } else {
            throw new MyException("年齡欄位不得為空白");
        }

        if (mail.length() > 0 && mail.length() < 25) {
            emp.seteEmail(input_eEmail.getText());
        } else {
            throw new MyException("信箱欄位不得為空白");
        }
```