# 5.3) Using a method that throws a runtime exception

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZnZz-izwfiyGBS_8Q%2Fimage.png?alt=media\&token=765bdc89-f33b-479a-9120-c85c670a697b)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZnjwydZgB10WHY2RM%2Fimage.png?alt=media\&token=0e34e723-c08f-437d-b57a-580ef3d54693)

```java
public class FeelingHungryException extends RuntimeException {
	private static final long serialVersionUID = 1388446834933699295L;
}
```

```java
import java.time.LocalTime;

public class Trip {
	public void goTrekking(LocalTime startTime) {
		// compare curremt time and start time
		// throw feelhungry exception if diff > 2 hrs
		int hrs = LocalTime.now().getHour() - startTime.getHour();
		if (hrs >= 2) {
			throw new FeelingHungryException();
		}
	}
}
```

```java
import java.time.LocalTime;

public class TestTrip {
	public static void main(String[] args) {
		Trip trip = new Trip();
		trip.goTrekking(LocalTime.of(11, 24));
	}
}
```

#### Output :

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZpqihbKkz0wcEkWNv%2Fimage.png?alt=media\&token=f99049b7-032a-4ec0-b018-72aa44d7df5b)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZqC-BaOwoWSRo_vTJ%2Fimage.png?alt=media\&token=a7569ad2-6233-452a-adff-f77c7bf21d40)

```java
public class InvalidArrayAccess {
	public static void main(String[] args) {
		String[] students = { "Shreya", "Joseph" };
		System.out.println(students[5]);
		System.out.println("All seems to be well");
	}
}
```

#### Output:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZqkKnU1fAxrmGXON5%2Fimage.png?alt=media\&token=6ba98b4e-9203-490e-9b7d-b354cfc90ccb)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZqpmqGo0VQFeOKCnW%2Fimage.png?alt=media\&token=ba79e286-37cd-42af-9efe-0d06b2edf852)

```java
public class InvalidArrayAccess {
	public static void main(String[] args) {
		String[] students = { "Shreya", "Joseph" };
		try {
			System.out.println(students[5]);
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("Exception");
		}
		System.out.println("All seems to be well");
	}
}
```

#### Output:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZr6t1fVZyyK6LYx5d%2Fimage.png?alt=media\&token=43231215-cb83-4a99-9794-23dd5ca2ff98)

#### Important Point:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LaZmp6ucQ7SGhF04f9h%2F-LaZrD5FyApoxheuK6XZ%2Fimage.png?alt=media\&token=6e60fbbd-6b4a-469e-82cf-f01d5901c337)

```java
public class InvalidArrayAccess {
	public static void main(String[] args) {
		String[] students = { "Shreya", "Joseph" };
		int pos = 10;
		if (pos > 0 && pos < students.length) {
			System.out.println(students[pos]);
		}
	}
}
```
