Could this perhaps be written in a different way without requiring a try-catch?