add exception catch to encoding functions