Error in calculation of second moment What is wrong with the following method of calculating second moments using integration by parts
$$\int_0^\infty x^2f(x)dx=[x^2F(x)]^\infty_0-2\int^\infty_0xF(x)dx=\infty-\infty$$
On the other hand taking $-P(X>x)$ as the integral of $f(x)$
$$-[x^2(1-F(x)]^\infty_0+2\int_0^\infty xP(X>x)dx=2\int_0^\infty xP(X>x)dx$$ provided the first term vanishes which we can assume it does.
 A: Let us treat each case separately. 

What is wrong with the following method of calculating second moments using integration by parts $$\int_0^\infty x^2f(x)dx=[x^2F(x)]^\infty_0-2\int^\infty_0xF(x)dx=\infty-\infty$$

We should always avoid expressions such as $\ \infty-\infty, \ $ $\frac{0}{0}, \ $ or $\ 0\cdot \infty \ $ while working with limits. This is because even if starting in reasonable place, we can end in completely unjustified or meaningless one.
For example, it is not always true that for all sequences $a_{n},b_{n}$
$$\lim_{n\to \infty}(a_{n}+b_{n})=\lim_{n\to \infty}a_{n}+\lim_{n\to \infty}b_{n}.$$ 
Take for example $a_{n}=n+1$, $b_{n}=-n$. Then
$$1=\lim_{n\to \infty}(n+1-n)\not=\lim_{n\to \infty}(n+1)-\lim_{n\to \infty}n=\infty - \infty = ?.$$ 
In our case note that 
$$\int_{0}^{\infty}x^{2}f(x)\mathrm{d}x := \lim_{n \to \infty}\int_{0}^{n}x^{2}f(x)\mathrm{d}x=\lim_{n\to \infty}\Bigg[[x^2F(x)]^n_0-2\int^n_0xF(x)\mathrm{d}x\Bigg].$$

On the other hand taking $−P(X>x)$ as the integral of $f(x)$
$$-[x^2(1-F(x)]^\infty_0+2\int_0^\infty xP(X>x)dx=2\int_0^\infty xP(X>x)dx$$

This is actually true. Even more general fact can be proven: 

Let $\ X \ $ be a nonnegative random variable and $ \ g:\mathbb{R}→\mathbb{R} \ $ a nonnegative strictly increasing differentiable function. Then
  $$\mathbb{E}g(X)=g(0)+\int_{0}^{\infty}g^{\prime}(x)\mathbb{P}(X>x)dx$$

Look here Expectation of nonnegative random variable when passed through nonnegative increasing differentiable function
