In the context of cancer, pneumothorax can occur as a complication of both primary cancer and metastatic cancer. Cancers such as lung cancer, mesothelioma, and metastatic cancers to the lung can directly cause pneumothorax by invading or damaging lung tissue.