# 5.1) Creating try-catch-finally blocks

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZc3ODmgr-_JeQDWt9%2F-LaZdYHilWd8hJEXfbw0%2Fimage.png?alt=media\&token=4fb2a71e-6b4b-4129-9671-bff2b9d4591d)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZc3ODmgr-_JeQDWt9%2F-LaZdlBkNx55vScPvhDV%2Fimage.png?alt=media\&token=7e17c3c4-e8ce-4444-adbe-deeb0b2ed5b4)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZjes1BKtgv5-9JG1q%2Fimage.png?alt=media\&token=d19befff-d61e-49dd-be5f-f27d32acb4bd)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZjrJcvyE2B5j3eev_%2Fimage.png?alt=media\&token=a6f605ee-9c37-4321-8c68-45deea727aa4)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZk-ciLcEAejWVGfZP%2Fimage.png?alt=media\&token=c3311e5d-4445-4943-88e2-7f8381367270)

```java
public class FallInRiverException extends Exception {
	private static final long serialVersionUID = 6674801099786714691L;
}
```

```java
public class DropOarException extends Exception {
	private static final long serialVersionUID = -5159676509920958459L;
}
```

```java
public class RiverRafting {
	public void crossRapid(int degree) throws FallInRiverException {
		System.out.println("Cross Rapid");
		if (degree > 10) {
			throw new FallInRiverException();
		}
	}

	public void rowRaft(String state) throws DropOarException {
		System.out.println("Row Raft");
		if (state.equals("nervous")) {
			throw new DropOarException();
		}
	}
}
```

```java
public class TestRiverRafting {
	public static void main(String[] args) {
		RiverRafting riverRafting = new RiverRafting();
		try {
			riverRafting.crossRapid(11);
			riverRafting.rowRaft("happy");
			System.out.println("Enjoy River Rafting");
		} catch (FallInRiverException e1) {
			System.out.println("Get back in the raft");
		} catch (DropOarException e2) {
			System.out.println("Do not panic");
		} finally {
			System.out.println("Pay for the sport");
		}
		System.out.println("After the try block");
	}
}
```

#### Output:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZl3_F-SA7Bfq6i6bj%2Fimage.png?alt=media\&token=5e2d07e6-f97d-46f0-ac3c-bb3f58534d10)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZk4aPmrFbQwmSuo6C%2Fimage.png?alt=media\&token=8d55fe66-37b3-4e74-a88e-d832cd861946)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZk8aEPQ9HuIrrqv0R%2Fimage.png?alt=media\&token=10c6d4dd-817e-4014-8b25-9e770f5780c9)

```java
public class TestRiverRafting {
	public static void main(String[] args) {
		RiverRafting riverRafting = new RiverRafting();
		try {
			riverRafting.crossRapid(7);
			riverRafting.rowRaft("happy");
			System.out.println("Enjoy River Rafting");
		} catch (FallInRiverException e1) {
			System.out.println("Get back in the raft");
		} catch (DropOarException e2) {
			System.out.println("Do not panic");
		} finally {
			System.out.println("Pay for the sport");
		}
		System.out.println("After the try block");
	}
}
```

#### Output:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZlG1KdAICqZCcNDze%2Fimage.png?alt=media\&token=9ffbeffc-dca2-4f92-9b15-dff8f878aec5)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZjdYQkHSJRiciA6Sn%2F-LaZkEfSnIkRaRCFqdHY%2Fimage.png?alt=media\&token=5f0c4212-4c9f-441c-a5f6-8eea3f6822d0)

