Исключение выбрасывается при отрицательном, но не принимает.
По условию, конечно, немного не так и моё решение похоже на костыль, но истина, чувтствую, где-то рядом.
package com.javarush.task.task14.task1420;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
НОД
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int first = Integer.parseInt(reader.readLine());
int second = Integer.parseInt(reader.readLine());
if (first < 0 || second < 0) throw new Exception();
System.out.println(gcd(first, second));
}
public static int gcd(int a, int b) {
while (b != 0) {
int tmp = a % b;
a = b;
b = tmp;
}
return a;
}
}