public class FeelingHungryException extends RuntimeException {
private static final long serialVersionUID = 1388446834933699295L;
}
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();
}
}
}
import java.time.LocalTime;
public class TestTrip {
public static void main(String[] args) {
Trip trip = new Trip();
trip.goTrekking(LocalTime.of(11, 24));
}
}
Output :
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:
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:
Important Point:
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]);
}
}
}