Change port file handling from try-catch to if-else