---
title: "Catch ex As Exception When..."
url: "https://osherove.com/blog/2003/8/12/catch-ex-as-exception-when.md"
canonical: "https://osherove.com/blog/2003/8/12/catch-ex-as-exception-when.html"
published: "2003-08-12T20:39:00"
updated: "2003-08-12T20:39:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "I too would like to add a cool little thingie in VB.Net after reading Rachel's post . One of the coolest exception handling features (which I could not find in C# BTW) is catching exceptions only on certain conditions.…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Catch ex As Exception When...

By Roy Osherove August 12, 2003 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published August 12, 2003; technical details may be outdated.

I too would like to add a cool little thingie in VB.Net after reading [Rachel's post](http://weblogs.asp.net/rreese/posts/23841.aspx).

One of the coolest exception handling features (which I could not find in C# BTW) is catching exceptions only on certain conditions. So I can basically write:

Private Sub Button1\_Click(By Val sender As System.Object, By Val e As System.Events) Handles Button.Click Try 'my bad code here  Catch ex As Exception **When false** 'we will never get here

End Try

End Sub

How cool is that? And does anyone know the equivalent to this in C#?

**Update:**

For some reason - some exceptions are never caught by my error handler. Division by zero for example. Take a look at this code:

Try Dim x As Integer = CInt(Textbooks.Text) 'x is zero MsgBox(x / x) Catch ex As Exception 'we never get here! 'whaaa? Me.Text = ex.Message End Try

I'm sure it's probably a setting somewhere in VS, but I can't seem to find it. :( Sometimes it's the simplest things that drive you crazy... 

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Aug 17, 2003

    ### [CodeSmart.Net](https://osherove.com/blog/2003/8/17/codesmartnet.html)

- Aug 17, 2003

    ### [WMI mind blower](https://osherove.com/blog/2003/8/17/wmi-mind-blower.html)

- Aug 12, 2003

    ### [My new favorite book](https://osherove.com/blog/2003/8/12/my-new-favorite-book.html)

- Aug 12, 2003

    ### [Caching in WinForms](https://osherove.com/blog/2003/8/12/caching-in-winforms.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
