     location = [line: line, file: Path.relative_to_cwd(file)]
     stack    = Macro.escape [{ module, :__MODULE__, 0, location }]
 
-    exc_filter_fn = fn
+    if multiple_exceptions?(exprs) do
+      { fun, arity } = fun_arity
+      raise Error, message: "multiple exceptions in one doctest case are not supported. "
+                            "Invalid doctest for #{inspect module}.#{fun}/#{arity}"
+    end
+
+    tests = Enum.map exprs, fn { expr, expected } ->
+      test_case_content(expr, expected, module, line, file, stack)
+    end
+
+    quote do
+      unquote_splicing(test_import(module, do_import))
+      unquote(gen_code_for_tests(tests, whole_expr(exprs), exception_expr(exprs), stack))
+    end
+  end
+
+  defp whole_expr(exprs) do
+    Enum.map_join(exprs, "\n", &elem(&1, 0))
+  end
+
+  defp exception_expr(exprs) do
+    Enum.find_value(exprs, "nothing", fn
+      { _, {:error, exception, message} } ->
+        inspect(exception) <> " with message " <> message
+      _ ->
+        nil
+    end)
+  end
+
+  defp multiple_exceptions?(exprs) do
+    Enum.count(exprs, fn
       { _, {:error, _, _} } -> true
       _ -> false
-    end
-
-    exceptions_num = Enum.count exprs, exc_filter_fn
-    if exceptions_num > 1 do
-      # Format the info about error location as if it were a part of the stacktrace
-      { fun, arity } = fun_arity
-      error_info = "    #{file}:#{line}: #{inspect module}.#{fun}/#{arity}"
-      raise Error, message: "multiple exceptions in one doctest case are not supported.\n#{error_info}"
-    end
-
-    { tests, whole_expr } = Enum.map_reduce exprs, "", fn {expr, expected}, acc ->
-      { test_case_content(expr, expected, module, line, file, stack), acc <> expr <> "\n" }
-    end
-    whole_expr = String.strip(whole_expr)
-
-    exception = case Enum.find(exprs, exc_filter_fn) do
-      { _, {:error, exception, message} } ->
-        inspect(exception) <> " with message " <> message
-      nil ->
-        "nothing"
-    end
-
-    quote do
-      unquote_splicing(test_import(module, do_import))
-      unquote(gen_code_for_tests(tests, whole_expr, exception, stack))
-    end
+    end) > 1
   end
 
   defp gen_code_for_tests(tests, whole_expr, exception, stack) do

  defmacro doctest(mod, opts // []) do
    quote bind_quoted: binding do
      lc { name, test } inlist ExUnit.DocTest.__doctests__(mod, opts) do
        def unquote(name)(_), do: unquote(test)
      end
    end

    moduledocs ++ docs
  end

  defp extract_from_moduledoc({_, doc}) when doc in [false, nil], do: []

  defp extract_from_moduledoc({line, doc}) do
    extract_tests(line, doc)
  end

  defp extract_from_doc({_, _, _, _, doc}) when doc in [false, nil], do: []

  defp extract_from_doc({ fa, line, _, _, doc}) do
    lc test inlist extract_tests(line, doc) do
