Throw error on invalid cast