It is legal to throw the ball off the backboard as a pass to yourself. The only time it is illegal is when attempting a free throw. While attempting a free throw the ball must hit the rim as well.
For reference, see Section III - Dribble of NBA Rule 10.
For the NCAA, it is scored as follows (from page 10 of the 2011 Official Basketball Statisticians’ Manual):
A.R.15. Adams throws a pass to himself or herself off the backboard, and then shoots and makes the basket. Ruling: Credit Adams with a FGA and FGM, but no assist or rebound.
For the NBA, according to this Q&A (from 2009) with Bernie Fryer (Vice President of Referee Operations and Director of Officials in the NBA), it is also allowed:
Are you allowed to throw the ball off the backboard, grab your own rebound and dunk it without landing? -- Blaise Bernie's answer: Yes. Under the traveling rule, a player who attempts a shot or pass may not be the first to touch the ball unless it touches the backboard, rim or another player. Therefore, a player can intentionally pass the ball to himself off the backboard or rim.
Actually, a week or so ago Kobe Bryant was double teamed near the 3-point line, had no one to pass to and had already picked up his dribble. He was about to fall down, which would have been traveling, instead he alertly threw the ball off the backboard, caught it and dribbled back to the top of the key. This wasn't an alley-oop but was similar. Michael Jordan was famous for doing the same thing on occasion.
It is definitely legal in the NBA. I'm not sure about the NCAA.
source: YouTube video of Kobe Bryant
While you can catch your own air ball, it must be an attempted shot, and not an attempt to pass to yourself. Generally it's clear what the player was attempting to do. When it's not clear, it's up to the referee's judgement what the player's intent was. I am a certified official in high school and college.
In FIBA games, deliberately throwing the ball on the backboard without attempting a shot is equivalent to the player bouncing the ball on the floor.
It is then allowed only if you hadn't dribbled yet, in which case, you can dribble no more after you catch the ball again, but you can only pass or shoot. In all other circumstances (for example if you had already dribbled, or if you start dribbling after catching the ball) is a double dribble violation.
See art. 24 of FIBA's "Offical Basketball Rules 2017 - Official Interpretations - valid as of 1st February 2018".
Art. 24 Dribbling
24-1 Statement. If a player deliberately throws the ball against a backboard (not attempting a legitimate shot for a field goal) this is considered as if the player has bounced the ball on the floor. If the player then touches the ball again before it has touched (or been touched by) another player, this is considered as a dribble.
24-2 Example: A1 has not yet dribbled when A1 throws the ball against the backboard and catches it again before another player has touched the ball.
Interpretation: After catching the ball A1 may shoot or pass but may not begin a new dribble.
24-3 Example: After ending a dribble either in the continuous motion or standing still, A1 throws the ball against the backboard and catches or touches it again before it has touched another player.
Interpretation: A1 has committed a double dribble violation.