diff --git a/src/bin/pgbench/t/001_pgbench_with_server.pl b/src/bin/pgbench/t/001_pgbench_with_server.pl
index 781cc08fb1..6a82a3494d 100644
--- a/src/bin/pgbench/t/001_pgbench_with_server.pl
+++ b/src/bin/pgbench/t/001_pgbench_with_server.pl
@@ -1190,29 +1190,32 @@ sub check_pgbench_logs
 	ok(@logs == $nb, "number of log files");
 	ok(grep(/\/$prefix\.\d+(\.\d+)?$/, @logs) == $nb, "file name format");
 
-	my $log_number = 0;
 	for my $log (sort @logs)
 	{
-		# Check the contents of each log file.
-		my $contents_raw = slurp_file($log);
+		eval {
+			# Check the contents of each log file.
+			my $contents_raw = slurp_file($log);
 
-		my @contents = split(/\n/, $contents_raw);
-		my $clen     = @contents;
-		ok( $min <= $clen && $clen <= $max,
-			"transaction count for $log ($clen)");
-		my $clen_match = grep(/$re/, @contents);
-		ok($clen_match == $clen, "transaction format for $prefix");
+			my @contents = split(/\n/, $contents_raw);
+			my $clen     = @contents;
+			ok( $min <= $clen && $clen <= $max,
+				"transaction count for $log ($clen)");
+			my $clen_match = grep(/$re/, @contents);
+			ok($clen_match == $clen, "transaction format for $prefix");
 
-		# Show more information if some logs don't match
-		# to help with debugging.
-		if ($clen_match != $clen)
-		{
-			foreach my $log (@contents)
+			# Show more information if some logs don't match
+			# to help with debugging.
+			if ($clen_match != $clen)
 			{
-				print "# Log entry not matching: $log\n"
-				  unless $log =~ /$re/;
+				foreach my $log (@contents)
+				{
+					print "# Log entry not matching: $log\n"
+					  unless $log =~ /$re/;
+				}
 			}
-		}
+		};
+		# Tell if an exception was raised
+		ok(0, "Error while checking log file \"$log\": $@") if $@;
 	}
 	return;
 }
