<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/138939>138939</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Clang++ rejects try-catch in discarded if constexpr branch with -fno-exceptions
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          zerhud
      </td>
    </tr>
</table>

<pre>
    **Summary**:
Clang incorrectly rejects code containing a `try-catch` block inside a discarded `if constexpr` branch when compiled with `-fno-exceptions`. The `try-catch` block should not be evaluated, as it is excluded at compile-time, but Clang emits an error: `cannot use 'try' with exceptions disabled`.

**Steps to Reproduce**:
1. Create a file `test.cpp` with the following code:
 ```cpp
   template<auto enable> void foo(auto&& fnc) {
       if constexpr(enable) try{ fnc(); } catch(...){}
       else fnc();
   }

 int main(int, char**) {
       foo<false>([]{});
   }
   ```
2. Compile with:
   ```bash
   clang++ -std=c++20 -fno-exceptions -c test.cpp
 ```
3. Alternatively, see the issue on [Godbolt](https://godbolt.org/z/3ecnde5z4): https://godbolt.org/z/3ecnde5z4

**Actual Results**:
```
test.cpp:2:25: error: cannot use 'try' with exceptions disabled
    if constexpr(enable) try{ fnc(); } catch(...){}
                         ^
1 error generated.
```

**Expected Results**:
The code should compile successfully, as the `try-catch` block is in a discarded `if constexpr` branch and should not be evaluated.

**Additional Information**:
- The code compiles successfully with GCC
- A not called template function with try-catch block rejected too ([Godbolt](https://godbolt.org/z/4nb3vcYTo))
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVUuP4ygQ_jXkUorl4CR2Dj446c5or7Nz2SOGcswuBgtw-vHrV2Xc6XTPtjQjrdWt-FFQ3wM-RAj6YhFrtjuy3cNKTLF3vn5F309q1Tr1UjPeMN78OQ2D8C_pgRUNy5uTEfYC2krnPcpoXsDj3yhjAOkUgnQ2Cm21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