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.)
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine. RuntimeException and its subclasses are unchecked exceptions.
The errors that occur during the execution of a program are called the runtime errors or the exceptions. Some of the examples of runtime erros are Division by Zero, Stack overflow, Invalid type casting, and File not found. Object-Oriented way of error handling is, Classes to handle different types of errors.
Catching Exception or ThrowableCatching Exception will catch both checked and runtime exceptions. Runtime exceptions represent problems that are a direct result of a programming problem, and as such shouldn't be caught since it can't be reasonably expected to recover from them or handle them.
Additionally, catching RuntimeException is considered as a bad practice. And, thus, throwing Generic Exceptions/Throwable would lead the developer to catch the exception at a later stage which would eventually lead to further code smells.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With