5.1) Creating try-catch-finally blocks
Previous5)What happens when an exception is thrown?Next5.2) Using a method that throws a checked exception
Last updated
Last updated
public class FallInRiverException extends Exception {
private static final long serialVersionUID = 6674801099786714691L;
}
public class DropOarException extends Exception {
private static final long serialVersionUID = -5159676509920958459L;
}
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();
}
}
}
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");
}
}
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");
}
}