https://chatgpt.com/share/31be55da-2e7f-4dc8-9fc3-000384f58086

Sample - 1
import kotlinx.coroutines.*
 
fun main() = runBlocking {
    val scope = CoroutineScope(Dispatchers.Default)
 
    val job = scope.launch {
        println("Job started")
        delay(1000)
        println("Job completed")
    }
 
    delay(500)  // Wait for a while
    scope.cancel()  // Cancel the scope
 
    // Attempt to launch a new job in the cancelled scope
    val newJob = scope.launch {
        println("New job started")
        delay(1000)
        println("New job completed")
    }
 
    newJob.invokeOnCompletion { exception ->
        if (exception is CancellationException) {
            println("New job was cancelled due to scope cancellation")
        } else if (exception != null) {
            println("New job completed with exception: $exception")
        } else {
            println("New job completed successfully")
        }
    }
 
    // Wait for all jobs to complete
    joinAll(job, newJob)
}
Sample - 2
import kotlinx.coroutines.*
 
fun main() = runBlocking {
    val scope = CoroutineScope(Dispatchers.Default)
 
    val job = scope.launch {
        println("Job started")
        delay(1000)
        println("Job completed")
    }
 
    delay(500)  // Wait for a while
    scope.cancel()  // Cancel the scope
 
    // Create a new scope and launch a new job
    val newScope = CoroutineScope(Dispatchers.Default)
    val newJob = newScope.launch {
        println("New job started")
        delay(1000)
        println("New job completed")
    }
 
    // Wait for all jobs to complete
    joinAll(job, newJob)
}