Question

Does C# have the equivalent of Java's java.lang.RuntimeException?

(I.E. an exception that can be thrown without the necessity of being caught, or the program crashing when the exception is thrown.)

Was it helpful?

Solution

SystemException is the equivalent, it is the base class of all exceptions that can be raised by .NET code. As opposed to application exceptions.

From the comments it however sounds like you want to catch this exception. In which case you should never use SystemException, you'll catch too many. Make your own exception class, derived from Exception.

There are no exception specifications in .NET, in case that's what you're after.

OTHER TIPS

In .NET, the program flow will fail if an unhandled exception occurs; a windows app will give up, and an ASP.NET application will bubble all the way to the Global.asax' Application_Error handler.

In that respect, no. However, perhaps you can include an example of what you're trying to do, and we can provide suggestions on patterns or approaches to get you a solution.

I don't think it does. But, why do you want one? Most likely, you can achieve your goal with some other part of the .NET system.

.NET does not have something exactly like this, but you can simulate it to some degree. You could, but many don't recommend, wrapping the entry point of your application in one single try/catch, and catch the exception you are looking for, and swallow it. If something closer catches it, then great.

This is built in some ways, like the DispatcherUnhandledException event in WPF; you can handle the event, and set the DispatcherUnhandledExceptionEventArgs.Handled = true to keep the application running.

In general, I don't recommend this pattern. Use Trace.WriteLine instead, and if you need to, write a Trace listener. Remember: Exceptions are exceptional. Something extraordinary happens that would mean it is better to stop the application than continue in a unpredicted state.

  1. C# does not have checked exceptions.

  2. Pick the most appropriate exception from this list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top