# 3) Creating a method that throws an exception

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZGySOeoO2wWveA-EV%2F-LaZGz20iXzSm0kUn3zE%2Fimage.png?alt=media\&token=c89fdb28-7856-41e1-a50d-46f22b1d6fbe)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZGySOeoO2wWveA-EV%2F-LaZH2jdxUIPhouIae4R%2Fimage.png?alt=media\&token=28b303c5-5a43-41b0-87e8-379e096489d2)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZGySOeoO2wWveA-EV%2F-LaZHJSix1xP0elEH3E7%2Fimage.png?alt=media\&token=0914f79b-d813-4271-ac9a-e0512a487694)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZGySOeoO2wWveA-EV%2F-LaZJBGxz6iKhOnawCsf%2Fimage.png?alt=media\&token=bb02f612-9c67-48eb-bd7e-386c255aa477)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZGySOeoO2wWveA-EV%2F-LaZJFCGhr89wO9dL7JX%2Fimage.png?alt=media\&token=ed723f24-4eee-4575-877e-f82e13299457)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZGySOeoO2wWveA-EV%2F-LaZJPs9aCm6_cpwQdNi%2Fimage.png?alt=media\&token=bc41b9c7-e227-42f2-95e4-fd7444456b74)

### Example 1:

```java
import java.io.FileNotFoundException;

public class DempThrowsException {
	public void readFile(String file) throws FileNotFoundException {
		boolean found = findfile(file);
		if(!found) {
			throw new FileNotFoundException("Missing file");
		}
	}

	private boolean findfile(String file) {
		// return true if file is located
		return false;
	}
}
```

### Example 2 :

```java
import java.io.FileNotFoundException;

public class DempThrowsException {
	public void readFile(String file) throws FileNotFoundException {
		boolean found = findfile(file);
		if(!found) {
			checkAndThrowException();
		}
	}

	private void checkAndThrowException() throws FileNotFoundException {
		throw new FileNotFoundException("Missing file");	
	}

	private boolean findfile(String file) {
		// return true if file is located
		return false;
	}
}
```

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZKHNm6Kmr-CHLtHtZ%2F-LaZKISopsw9lD6Oxh5I%2Fimage.png?alt=media\&token=50376f0f-a3c2-4090-8bbe-b993ae3548ba)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZKnQ-L-XyilG3WH8t%2F-LaZKoCACzRmbvdjLa3o%2Fimage.png?alt=media\&token=26ed9edf-5ebd-4c1b-8648-9b046ab202d3)

#### 1) Handle the exception :

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZKnQ-L-XyilG3WH8t%2F-LaZL1-hP8OttyvMn_xv%2Fimage.png?alt=media\&token=e027ef8a-156b-4407-a1e3-c9955c853bb0)

#### 2) Declare it to be thrown:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZKnQ-L-XyilG3WH8t%2F-LaZLCfNdQvnGZGvkRAa%2Fimage.png?alt=media\&token=29cbbd9b-6f07-47ca-bece-bc278ac7a3c2)

#### 3) Handle and declare:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZKnQ-L-XyilG3WH8t%2F-LaZLOZ7MTwOWy-QUZFa%2Fimage.png?alt=media\&token=2d1d13bd-5076-4f08-bacd-34e01b99d354)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZOBW4UX21Lj3_0wv6%2F-LaZODwEwQbPaGROIVcu%2Fimage.png?alt=media\&token=b2394a47-62de-49d6-a32d-5cb2ad32e344)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZOBW4UX21Lj3_0wv6%2F-LaZOMprm79dYgz0tDLU%2Fimage.png?alt=media\&token=c1cdaa9a-3940-417f-8b57-5f9a3574ad96)

### Example 3:

```java
import java.io.FileNotFoundException;

public class DempThrowsException {
	public void readFile(String file) throws FileNotFoundException {
		if (file == null) {
			throw new NullPointerException();
		}
		boolean found = findfile(file);
		if (!found) {
			throw new FileNotFoundException("Missing file");
		}
	}

	private boolean findfile(String file) {
		// return true if file is located
		return false;
	}
}
```

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZOBW4UX21Lj3_0wv6%2F-LaZOmQmb6IyhTL_qqsa%2Fimage.png?alt=media\&token=e5a4f52f-b998-4b17-a167-a1f1a3601d74)

### Example 4:

```java
import java.io.FileNotFoundException;

public class ThrowExceptions {
	void method1() throws Throwable {

	}

	void method2() throws Error {

	}

	void method3() throws Exception {

	}

	void method4() throws RuntimeException {

	}

	void method5() throws FileNotFoundException {

	}
}
```

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZOBW4UX21Lj3_0wv6%2F-LaZPZvq4c6_2dYbx5dD%2Fimage.png?alt=media\&token=f08f64e3-12dc-4a3a-b3c0-a3b878d0b22d)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZOBW4UX21Lj3_0wv6%2F-LaZPlocrLix0QBqWS6-%2Fimage.png?alt=media\&token=02a19d63-be8e-45b2-ba4a-b246602c7ebf)

### Example 5:

```java
import java.io.FileNotFoundException;

public class ThrowExceptions {
	void method1() throws Throwable {

	}

	void method2() throws Error {

	}

	void method3() throws Exception {

	}

	void method4() throws RuntimeException {

	}

	void method5() throws FileNotFoundException {

	}

	void method6() {
		try {

		} catch (Throwable e) {

		}

	}

	void method7() {
		try {

		} catch (Error e) {

		}
	}

	void method8() {
		try {

		} catch (Exception e) {

		}
	}

	void method9() {
		try {

		} catch (RuntimeException e) {

		}
	}

	void method10() {
		try {

		} catch (FileNotFoundException e) {

		}
	}
}
```
