How is the integral calculator finding this $\int_{0}^{a}\frac{a^2-x^2}{(a^2+x^2)^2}dx$ while I get an undefined expression? My book's attempt:
$$\int_{0}^{a}\frac{a^2-x^2}{(a^2+x^2)^2}dx$$
Let us find the antiderivative first:
$$\int\frac{a^2-x^2}{(a^2+x^2)^2}dx$$
$$=\int\frac{x^2(\frac{a^2}{x^2}-1)}{(x(\frac{a^2}{x}+x))^2}dx\tag{1}$$
$$=\int\frac{x^2(\frac{a^2}{x^2}-1)}{x^2(\frac{a^2}{x}+x)^2}dx$$
$$=\int\frac{(\frac{a^2}{x^2}-1)}{(\frac{a^2}{x}+x)^2}dx\tag{2}$$
$$=-\int\frac{1}{(\frac{a^2}{x}+x)^2}d(\frac{a^2}{x}+x)$$
$$=\frac{1}{(\frac{a^2}{x}+x)}+C\tag{3}$$
$$=\frac{x}{a^2+x^2}+C\tag{4}$$

$$\left[\frac{1}{(\frac{a^2}{x}+x)}\right]_{0}^{a}=\text{undefined}$$
$$\left[\frac{x}{a^2+x^2}\right]_{0}^{a}=\frac{1}{2a}\tag{5}$$
$(5)$ gives the correct answer according to this integral-calculator.

Both $(3)$ and $(4)$ should be perfectly valid antiderivatives, as we have completed the integration process before $(3)$. However, I found that only $(4)$ is a usable antiderivative, and $(3)$ gives an undefined expression.
Question:

*

*Why do we get an undefined expression using $(3)$?

My hunch:
My book's attempt of finding the antiderivative is wrong in that my book did $\frac{0}{0}=1$ multiple times throughout its working (for example, in lines $(1)$ and $(2)$). We should expect $(3)$ not to work since the process was wrong. When going from $(3)$ to $(4)$, the book did $\frac{0}{0}=1$ again. I am surprised that the book somehow managed to land at the correct antiderivative for our domain given that the process was wrong, and going from $(3)$ to $(4)$ is wrong as well.
 A: You get an error with (3) because evaluating the expression at 0 results into a division-by-zero error. Now the standard way to bypass this problem is to replace 0 by $\varepsilon$ and then let $\varepsilon$ goes to $0$. Note that both expressions (3) and (4) coincide when $0$ is replaced by $\varepsilon$. So they must have the same limit when $\varepsilon$ goes to zero and since expression (4) is continuous at zero, the limit is the value of (4) at zero.
A: You could say:
$$\left[\cfrac{1}{\left(\frac{a^2}{x}+x\right)}\right]_0^a=\frac{1}{a+a}-\lim_{x\to0^+}\frac{1}{\frac{a^2}{x}+x}=\frac1{2a}$$
so you get the same result either way. We can make this assumption since the domain of the integral is $[0,a]$ and so we know that $0$ is approached from above.
A: What the author does in step $(1)$ (which needs to be reversed in step $(4)\,)$ is an informal 'trickery' that works because the improper integral that implicitly gets created is convergent.
This is somewhat analogous to saying that $\tan x$ and $\cot x$ are reciprocals of each other, even though technically this is false for multiples of $\displaystyle\frac\pi2,$ at which the same sort of division-by-zero magic is nonetheless acceptable.

Alternatively—quoting GEdgar above—When you reach $(4),$ even if you have questionable (or even false) steps in there, you can check that is it the antiderivative by differentiating. This is a good idea, and a valid proof-by-verification argument.
Alternatively, both cloudy's suggestion and the following method (the third step uses $\theta=\operatorname{arcsinh}\frac xa$ and integration by parts) also avoid having to divide by $0:$ \begin{align}&\int\frac{a^2-x^2}{(a^2+x^2)^2}\,\mathrm dx\\=&\int\frac{2 a^2}{(a^2 + x^2)^2} - \frac1{a^2 + x^2}\,\mathrm dx\\=&\frac2a\int{\operatorname{sech}^3\left(\operatorname{arcsinh}\frac xa\right)}\,\mathrm d\left(\operatorname{arcsinh}\frac xa\right)-\frac1a\arctan\frac xa\\=&\frac1a\left(\operatorname{sech}\theta\tanh\theta+\int\operatorname{sech}\theta\,\mathrm d\theta\right)-\frac1a\arctan\frac xa\\=&\frac1a\left(\sinh\theta\operatorname{sech}^2\theta+\arctan\frac xa\right)-\frac1a\arctan\frac xa+C\\=&\frac1a\left(\frac xa\times\frac1{1+\left(\frac xa\right)^2}\right)+C\\=&\frac x{a^2+x^2}+C.\end{align}
Alternatively, use $\theta=\arctan \frac xa$ or integrate by reduction formula.
