Skip to content

Instantly share code, notes, and snippets.

@OlgaKulikova
Created October 11, 2014 22:49
Show Gist options
  • Save OlgaKulikova/3cad47b5f7d422ea240f to your computer and use it in GitHub Desktop.
Save OlgaKulikova/3cad47b5f7d422ea240f to your computer and use it in GitHub Desktop.
Придумать исключение
package MyExceptions;
// Придумать свое исключение и написать соответствующий класс и использующий его код.
import java.util.InputMismatchException;
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Введите количество пройденных шагов");
try {
int steps = scan.nextInt();
StepCounter st = new StepCounter();
double m = st.count(steps);
System.out.println("Вы прошли " + m + " метров");
} catch (StepException e) {
System.out.println(e.getMessage());
} catch (InputMismatchException ex) {
System.out.println("Не верный формат ввода!");
}
}
}
package MyExceptions;
public class StepCounter {
private static final double LENGTH_OF_ONE_STEP = 0.6;
public double count(int steps) throws StepException {
if (steps < 0) {
throw new StepException("wrong steps");
}
return steps * LENGTH_OF_ONE_STEP;
}
}
package MyExceptions;
public class StepException extends Exception {
public StepException(String message) {
super(message);
}
@Override
public String getMessage() {
return "StepException: " + super.getMessage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment