Why can't I sub $a=0$ into this expression? I was doing a maths problem from the Internet which involved the function $$J(a)=\int_{0}^{\infty}\frac{\cos(ax)}{1+x^2}\mathrm{d}x.$$
The aim of the problem was to use the relation $J''(a)-J(a)=0$ (differentiation with respect to $a$) to evaluate the following integral: $$I=\int_{-\infty}^{\infty}\frac{\cos(x)}{1+x^2}\mathrm{d}x.$$
However, part of the problem involved finding the value of $J'(0)$ to solve the above second order differential equation to find $J(a)$. The argument is as follows: $$\begin{align}J'(a) &=\int_{0}^{\infty}\frac{-x\sin(ax)}{1+x^2}\mathrm{d}x \\ &= \int_{0}^{\infty}\frac{-(x^2+1-1)\sin(ax)}{x(1+x^2)} \mathrm{d}x \\ &= -\int_{0}^{\infty}\frac{\sin (ax)}{x} \mathrm{d}x+\int_{0}^{\infty}\frac{\sin(ax)}{x(1+x^2)}\mathrm{d}x \end{align}.$$
By letting $a \rightarrow 0$ from the positive side, $J'(0)=-\pi/2$ using the standard result for $$\int_{0}^{\infty}\left(\frac{\sin(ax)}{x}\right)\mathrm{d}x = \pi/2, \text{ 
 for } a>0 $$ 
Then we can solve that differential equation, find that $$J(a)=(\pi/2)e^{-a}$$ for $a>0$ and hence find $I$.
My question is, why is it necessary to find $J'(0)$ in this way? It isn't clear to me why we can't just substitute $a=0$ into the first expression for $J'(a)$ and conclude $J'(0)=0$. 
I only have an informal understanding of limits, please keep that in mind.
 A: This is a very subtle issue. The main problem is illustrated by the final result, $J(a) = \frac{\pi}{2} \mathrm{e}^{-|a|}$ for $a \in \mathbb{R}$ . $J$ has a cusp at the origin, so (as mentioned by user in the comments) $J'(0)$ does not exist! In order to make sense of this calculation we need the notion of uniform convergence of improper integrals with a parameter, which is discussed here (PDF) in great detail.
Let us start with the function $J$ itself. The integral by which it is defined converges absolutely uniformly on $\mathbb{R}$ by the Weierstrass M-test, so $J$ is a continuous function on $\mathbb{R}$ . We also know that $J(0) = \frac{\pi}{2}$ and $J(-a) = J(a)$ hold for $a \in \mathbb{R}$ .
Now we need to find the derivative of $J$ . At the origin we consider the difference quotient
$$ \frac{J(a) - J(0)}{a} = - \operatorname{sgn}(a) \int \limits_0^\infty \frac{1-\cos(|a|x)}{|a|(1+x^2)} \, \mathrm{d} x  \stackrel{|a| x = y}{=} - \operatorname{sgn}(a) \int \limits_0^\infty \frac{1-\cos(y)}{a^2+y^2} \, \mathrm{d} y $$
for $a \in \mathbb{R} \setminus \{0\}$ . The final integral converges absolutely uniformly in $a \in \mathbb{R}$ by Weierstrass' test, so the one-sided limits of this expression exist and are given by
$$ \lim_{a \to 0^{\pm}} \frac{J(a) - J(0)}{a} = \mp \int \limits_0^\infty \frac{1-\cos(y)}{y^2} \, \mathrm{d} y = \mp \frac{\pi}{2} \, .$$
They do not agree, however, so $J$ is not differentiable at the origin as claimed. Since the integral
$$ \int \limits_0^\infty \frac{- x \sin(a x)}{1+x^2} \, \mathrm{d} x$$
converges uniformly in $a \in [r,\infty)$ and $a \in (-\infty,r]$ for every $r>0$ by Dirichlet's test, $J$ is differentiable on $\mathbb{R} \setminus \{0\}$ and we can interchange differentiation and integration to obtain
$$ J'(a) = \int \limits_0^\infty \frac{- x \sin(a x)}{1+x^2} \, \mathrm{d} x$$
for $a \in \mathbb{R} \setminus \{0\}$ . For the sake of simplicity we will focus on $a > 0$ from now on. We would like to compute $\lim_{a \to 0^+} J'(a)$ . Simply interchanging the limit and the integral sign (and concluding that the limit is zero) is not allowed here, since the integrand does not converge to zero uniformly as $a \to 0^+$ ! Instead, we need to rewrite the derivative (as you did in the question):
$$ J'(a) = - \frac{\pi}{2} + \int \limits_0^\infty \frac{\sin(a x)}{x (1+x^2)} \, \mathrm{d} x \, , \, a > 0 \, . $$
Now we have uniform convergence (the remaining integral is bounded by $\frac{\pi}{2} a$) and may conclude that $\lim_{a \to 0^+} J'(a) = - \frac{\pi}{2}$ holds (the limit is equal to the right derivative $\partial_+ J(0)$ at the origin).
We need to compute the second derivative from the second form of $J'$ as well, since only there the ensuing integral is (locally) uniformly convergent. We find $J''(a) = J(a)$ for $a > 0$, so $J \vert_{[0,\infty)}$ is the unique solution of the initial value problem
$$ \begin{cases} f''(a) &= f(a) \, , \, a > 0 \\ f(0) &= \frac{\pi}{2} \\ \partial_+ f(0) &= - \frac{\pi}{2} \end{cases} \, .$$
Therefore, $J(a) = \frac{\pi}{2} \mathrm{e}^{-a}$ holds for $a \geq 0$ and the result for arbitrary $a \in \mathbb{R}$ follows by symmetry.
Note that while the question of uniform convergence must be considered when computing $J''$ in any case, the problem with $J'(0)$ can be avoided by showing that $\lim_{a \to \infty} J(a) = 0$ holds (using integration by parts) and then obtaining $J \vert_{[0,\infty)}$ as the unique solution to the boundary value problem
$$ \begin{cases} f''(a) &= f(a) \, , \, a > 0 \\ f(0) &= \frac{\pi}{2} \\ \displaystyle{\lim_{a \to \infty}} f(a) &= 0 \end{cases} \, .$$
