Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java RuntimeException equivalent in C#?

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

like image 283
leeand00 Avatar asked Mar 23 '11 18:03

leeand00


People also ask

What is a RuntimeException in Java?

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.

What is runtime exception in C#?

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.

Can we catch RuntimeException in Java?

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.

Is it a good practice to catch a RuntimeException in Java?

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.


2 Answers

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.

like image 146
Hans Passant Avatar answered Oct 01 '22 23:10

Hans Passant


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.

like image 23
Tejs Avatar answered Oct 01 '22 23:10

Tejs