class Util { void goo(int a, int b) { try { a %= b; if (b == 0)return; } catch (Exception e) { if (e instanceof ArithmeticException){ System.out.println("expected"); } } } public static void main(String[] args, int d) { String is = null; if (d != 0) return; try { if (Math.random() > 0.5) { double k = 1 / d; } else { is = "This is printed half of the time"; double k = 1 / 0; } } catch (Exception ex) { ex.printStackTrace(); if (is != null) { System.out.println(is); } } } }