[BE]: Enable ruff rule TRY302 and apply fixes (#101874)

Removes useless try statements and unreachable code.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/101874
Approved by: https://github.com/malfet
diff --git a/test/distributed/test_dynamo_distributed.py b/test/distributed/test_dynamo_distributed.py
index 31a36e1..dc74b46 100644
--- a/test/distributed/test_dynamo_distributed.py
+++ b/test/distributed/test_dynamo_distributed.py
@@ -150,8 +150,6 @@
         DDP._active_ddp_module = self
         try:
             yield
-        except Exception:
-            raise
         finally:
             DDP._active_ddp_module = None
 
diff --git a/test/onnx/verify.py b/test/onnx/verify.py
index ac6f374..0dca467 100644
--- a/test/onnx/verify.py
+++ b/test/onnx/verify.py
@@ -66,13 +66,9 @@
         At the moment, only tests on "numpy.ndarray" are supported.
         """
         if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
-            try:
-                np.testing.assert_allclose(
-                    x, y, rtol=self.rtol, atol=self.atol, equal_nan=True, verbose=True
-                )
-            except AssertionError as e:
-                raise
-                k(f"{colonize(msg)}{str(e).lstrip()}")
+            np.testing.assert_allclose(
+                x, y, rtol=self.rtol, atol=self.atol, equal_nan=True, verbose=True
+            )
         else:
             raise RuntimeError("Unsupported almost equal test")
 
@@ -105,11 +101,7 @@
             new_msg = f"{colonize(msg)}In embedded parameter '{x.name}'"
             self.equalAndThen(t1, t2, new_msg, k)
         elif isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
-            try:
-                np.testing.assert_equal(x, y)
-            except AssertionError as e:
-                raise
-                k("{}{}".format(colonize(msg, ": "), str(e).lstrip()))
+            np.testing.assert_equal(x, y)
         else:
             if x != y:
                 # TODO: Better algorithm for lists
diff --git a/test/test_dynamic_shapes.py b/test/test_dynamic_shapes.py
index 4689795..2c30304 100644
--- a/test/test_dynamic_shapes.py
+++ b/test/test_dynamic_shapes.py
@@ -559,15 +559,12 @@
             lambda_apply = getattr(operator, fn)
 
         def guard_fn(v):
-            try:
-                if type(v) in (SymBool, bool):
-                    return guard_bool(v)
-                elif type(v) in (SymFloat, float):
-                    return guard_float(v)
-                else:  # SymInt, int
-                    return guard_int(v)
-            except Exception as e:
-                raise e
+            if type(v) in (SymBool, bool):
+                return guard_bool(v)
+            elif type(v) in (SymFloat, float):
+                return guard_float(v)
+            else:  # SymInt, int
+                return guard_int(v)
 
         # Get reference result
         with maybe_xfail(inp1, inp2):
diff --git a/test/test_mps.py b/test/test_mps.py
index d6e0564..1629da3 100644
--- a/test/test_mps.py
+++ b/test/test_mps.py
@@ -10487,52 +10487,47 @@
             # Forward check
             #
             forward_failed = False
-            try:
-                mps_sample = cpu_sample.transform(
-                    lambda x: x.detach().to("mps").requires_grad_(x.requires_grad) if isinstance(x, torch.Tensor) else x)
+            mps_sample = cpu_sample.transform(
+                lambda x: x.detach().to("mps").requires_grad_(x.requires_grad) if isinstance(x, torch.Tensor) else x)
 
-                cpu_args = [cpu_sample.input] + list(cpu_sample.args)
-                cpu_kwargs = cpu_sample.kwargs
-                mps_args = [mps_sample.input] + list(mps_sample.args)
-                mps_kwargs = mps_sample.kwargs
+            cpu_args = [cpu_sample.input] + list(cpu_sample.args)
+            cpu_kwargs = cpu_sample.kwargs
+            mps_args = [mps_sample.input] + list(mps_sample.args)
+            mps_kwargs = mps_sample.kwargs
 
-                # for tensor_split(), the second tensor arg ("tensor_indices_or_sections") must be on CPU only
-                if (op.name == "tensor_split" and isinstance(mps_args[1], torch.Tensor)):
-                    mps_args[1] = cpu_args[1]
+            # for tensor_split(), the second tensor arg ("tensor_indices_or_sections") must be on CPU only
+            if (op.name == "tensor_split" and isinstance(mps_args[1], torch.Tensor)):
+                mps_args[1] = cpu_args[1]
 
-                cpu_out = op(*cpu_args, **cpu_kwargs)
-                mps_out = op(*mps_args, **mps_kwargs)
+            cpu_out = op(*cpu_args, **cpu_kwargs)
+            mps_out = op(*mps_args, **mps_kwargs)
 
-                if (op.name in self.FP32_LOW_PRECISION_LIST) and dtype == torch.float32:
-                    atol = 1e-4
-                    rtol = 3e-5
-                elif op.name == "nn.functional.conv2d" or op.name == "linalg.multi_dot" and dtype == torch.float32:
-                    atol = 1e-4
-                    rtol = 3e-5
-                elif (op.name in self.FP16_LOW_PRECISION_LIST) and dtype == torch.float16:
-                    atol = 1e-2
-                    rtol = 1e-2
-                elif (op.name == "masked.mean"):
-                    atol = 7e-4
-                    rtol = 2e-3
-                elif (op.name == "native_layer_norm"):
-                    atol = 1e-4
-                    rtol = 1.3e-5
-                elif (op.name == "norm" or op.name == "linalg.norm") and dtype == torch.float16:
-                    atol = 7e-4
-                    rtol = 1.5e-3
-                elif op.name == "unique" and cpu_kwargs["sorted"] is False:
-                    continue
-                else:
-                    atol = None
-                    rtol = None
+            if (op.name in self.FP32_LOW_PRECISION_LIST) and dtype == torch.float32:
+                atol = 1e-4
+                rtol = 3e-5
+            elif op.name == "nn.functional.conv2d" or op.name == "linalg.multi_dot" and dtype == torch.float32:
+                atol = 1e-4
+                rtol = 3e-5
+            elif (op.name in self.FP16_LOW_PRECISION_LIST) and dtype == torch.float16:
+                atol = 1e-2
+                rtol = 1e-2
+            elif (op.name == "masked.mean"):
+                atol = 7e-4
+                rtol = 2e-3
+            elif (op.name == "native_layer_norm"):
+                atol = 1e-4
+                rtol = 1.3e-5
+            elif (op.name == "norm" or op.name == "linalg.norm") and dtype == torch.float16:
+                atol = 7e-4
+                rtol = 1.5e-3
+            elif op.name == "unique" and cpu_kwargs["sorted"] is False:
+                continue
+            else:
+                atol = None
+                rtol = None
 
-                self.assertEqual(cpu_out, mps_out, atol=atol, rtol=rtol)
+            self.assertEqual(cpu_out, mps_out, atol=atol, rtol=rtol)
 
-            except Exception as e:
-                raise e
-                forward_failed = True
-                all_forward_pass = False
 
             #
             # Backward check
diff --git a/test/test_transformers.py b/test/test_transformers.py
index bb27202..e4030fe 100644
--- a/test/test_transformers.py
+++ b/test/test_transformers.py
@@ -46,8 +46,6 @@
     try:
         torch.use_deterministic_algorithms(mode, warn_only=warn_only)
         yield {}
-    except RuntimeError as err:
-        raise err
     finally:
         torch.use_deterministic_algorithms(previous_mode, warn_only=previous_warn_only)