Repository: pylint-dev/pylint

Issue:
AttributeError: 'Lambda' object has no attribute 'bases' in exceptions checker
### Bug description

Pylint crashes on this fuzzed code from pysource-codegen:
```python
e = lambda x: 1, 2

try:
    pass
except e:
    pass
```

### Configuration

```ini

```

### Command used

```shell
pylint crash.py
```

### Pylint output

```python
Exception on node <Try l.3 at 0x7fea43cf8a10> in file 'pylint/crash.py'
Traceback (most recent call last):
  File "pylint/pylint/utils/ast_walker.py", line 87, in walk
    callback(astroid)
  File "pylint/pylint/checkers/exceptions.py", line 609, in visit_try
    self._check_catching_non_exception(handler, exception, part)
  File "pylint/pylint/checkers/exceptions.py", line 433, in _check_catching_non_exception
    if all(
       ^^^^
  File "pylint/pylint/checkers/exceptions.py", line 435, in <genexpr>
    and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "pylint/pylint/checkers/utils.py", line 1471, in has_known_bases
    for base in klass.bases:
                ^^^^^^^^^^^
AttributeError: 'Lambda' object has no attribute 'bases'
```

### Expected behavior

No crash

### Pylint version
pylint: 55098c7
astroid: cae4388d75
Python 3.12.7

### OS / Environment

Arch Linux

### Additional dependencies

```python

```


Discussion:

Patch:
diff --git a/doc/whatsnew/fragments/10106.bugfix b/doc/whatsnew/fragments/10106.bugfix
new file mode 100644
index 0000000000..7309e5b949
--- /dev/null
+++ b/doc/whatsnew/fragments/10106.bugfix
@@ -0,0 +1,3 @@
+Fix a crash when something besides a class is found in an except handler.
+
+Closes #10106
diff --git a/pylint/checkers/exceptions.py b/pylint/checkers/exceptions.py
index 1669e6e5b5..7c88c44c9b 100644
--- a/pylint/checkers/exceptions.py
+++ b/pylint/checkers/exceptions.py
@@ -432,7 +432,13 @@ def _check_catching_non_exception(
                 return
             if all(
                 node
-                and (utils.inherit_from_std_ex(node) or not utils.has_known_bases(node))
+                and (
+                    utils.inherit_from_std_ex(node)
+                    or (
+                        isinstance(node, nodes.ClassDef)
+                        and not utils.has_known_bases(node)
+                    )
+                )
                 for node in inferred
             ):
                 return


Test patch:
diff --git a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py
index 1b513f9724..0c3589c311 100644
--- a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py
+++ b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.py
@@ -132,3 +132,11 @@ class SomeBase(UnknownError):
     raise ValueError
 except EXCEPTIONS:
     pass
+
+
+LAMBDA = lambda x: 1, 2
+
+try:
+    pass
+except LAMBDA:  # [catching-non-exception]
+    pass
diff --git a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt
index 6d35a1e22e..905f35f129 100644
--- a/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt
+++ b/tests/functional/i/invalid/invalid_exceptions/invalid_exceptions_caught.txt
@@ -10,3 +10,4 @@ catching-non-exception:71:7:71:52::"Catching an exception which doesn't inherit
 catching-non-exception:84:7:84:26::"Catching an exception which doesn't inherit from Exception: NON_EXCEPTION_TUPLE":UNDEFINED
 catching-non-exception:102:7:102:13::"Catching an exception which doesn't inherit from Exception: object":UNDEFINED
 catching-non-exception:107:7:107:12::"Catching an exception which doesn't inherit from Exception: range":UNDEFINED
+catching-non-exception:141:7:141:13::"Catching an exception which doesn't inherit from Exception: LAMBDA":UNDEFINED