33

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.)

leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 1
    Unfortunately .net has no useful exception hierarchy. – CodesInChaos Mar 23 '11 at 18:48
  • 1
    Genuine question: what's the use of an exception if it doesn't crash the program when not caught?? – jeroenh Mar 23 '11 at 18:58
  • 4
    Op's definition of RuntimeException in Java is not quite right. A `RuntimeException` will crash the program if not caught. The difference with other exceptions is you do not need to declare a `throw` clause in your function prototype. It would be closer to C++ `runtime_error`, that is exceptions that are not supposed to happen, but sometimes do in very special circumstances and you don't want to bother with handling them in low level code. – J.N. Jun 28 '11 at 09:02

5 Answers5

28

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Okay good that's what I did. I derived it from Exception but I wasn't totally sure that's how it worked in C#. Thanks! – leeand00 Mar 24 '11 at 00:56
  • 1
    I am new at C#. In Java, runtime exceptions are generally not meant to be caught. This is because the purpose of runtime exceptions is to signalize some kind of a fatal programming error. (In case of a desktop application, it can be a good idea to use own [UncaughtExceptionHandler](http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.UncaughtExceptionHandler.html) to inform the user about the error and then terminate the application.) In C#, one can use [Debug.Assert](http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert.aspx), but only in Debug mode. – Tamás Jul 14 '13 at 23:01
  • That is not different. AppDomain.UnhandledException in .NET – Hans Passant Jul 14 '13 at 23:34
1

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.

Tejs
  • 40,736
  • 10
  • 68
  • 86
0

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.

John Fisher
  • 22,355
  • 2
  • 39
  • 64
  • I have an exception which I wish to throw, however it doesn't really matter if it occurs or not, it's not going to cause catastrophic failure throughout the whole program when it is thrown. – leeand00 Mar 23 '11 at 18:54
  • 2
    @leeand00: Should it really be an exception then..? Or some other pattern like raising an event, and letting whatever subscribe to the event. There are plenty of other patterns to do this other than exceptions. – vcsjones Mar 23 '11 at 19:29
0

.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.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
0
  1. C# does not have checked exceptions.

  2. Pick the most appropriate exception from this list.

Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124