Repository: sympy/sympy
File: sympy/physics/continuum_mechanics/beam.py (Python)

Code before:

        for expr, cond in moment_curve.as_expr_set_pairs():
            if expr.equals(0):
                continue

            try:
                sol = solveset(expr, x, domain=cond)
                if isinstance(sol, FiniteSet):
                    roots.update(sol)
                elif isinstance(sol, Union):
                    for part in sol.args:
                        if isinstance(part, FiniteSet):
                            roots.update(part)
            except NotImplementedError:
                continue

            if hasattr(cond, 'start') and hasattr(cond, 'end'):
                for pt in [cond.start, cond.end]:
                    if pt.is_real:
                        try:
                            val = expr.subs(x, pt).evalf()
                            if val == 0:
                                roots.add(pt)

                        except Exception:
                            pass

        # Checks for the sign change at roots
        epsilon = 1e-6
        points = []

        for point in sorted(roots, key=lambda r: r.evalf()):
            if not point.is_number:
                continue
            if point.evalf() <= 0 or point.evalf() >= self.length:
                continue

            try:
                left = bm.subs(x, point - epsilon).evalf()
                right = bm.subs(x, point + epsilon).evalf()

                # Strict condition that shows a shift in bending moment at roots
                if left * right < 0:
                    points.append(point)

            except Exception:
                continue

        # Returns Valid points of contraflexure
        return points


Review comment (suggestion):
There should not be code using `except Exception` in the library. This should catch the intended exception type only. Ideally it should not catch exceptions at all and should check ahead of time for the conditions that would otherwise raise an exception.

Code after:
        """

        x = self.variable
        l = self.length

        bm = self.bending_moment()

        # Filter out singularity terms with order < 0
        terms = []
        for term in bm.args:
            sfs = list(term.atoms(SingularityFunction))

            if not sfs:
                terms.append(term)
            else:
                for sf in sfs:
                    order = sf.args[2]
                    if order >= 0:
                        terms.append(term)

        non_singular_bending_moment = sum(terms)

        # Identify singularity points to define intervals
        singularity_points = set()
        for term in non_singular_bending_moment.atoms(SingularityFunction):
            location = term.args[1]
            singularity_points.add(location)
        singularity_points = sorted(singularity_points, key=lambda p: p.evalf())

        # Define intervals
        intervals = []
        points = [0] + [p for p in singularity_points if p != 0 and p != l] + [l]
        for i in range(len(points) - 1):
            start, end = points[i], points[i + 1]
            if start != end:
                intervals.append(Interval(start, end))

        # Solve for roots in each interval
        roots = set()
        for interval in intervals:
            # adding singularity points as default roots for temporary
            roots.add(interval.start)
            roots.add(interval.end)
            expr = non_singular_bending_moment

            sol = solveset(expr, x, domain=interval)
            if isinstance(sol, FiniteSet):
                for r in sol:
                    roots.add(r)
            elif isinstance(sol, Interval):
                roots.add(sol.start)