Are all types of exceptions caught under the regular old "exception"?

I'm just trying to figure it out a little better.

I understand that there are many different types of exceptions, and according to some reading I made, all types of exceptions fall into Exception. First of all, can I be sure that this is true?

try{ ... } catch(Exception x){ //No matter what fails in the try block, x //will always have a value since all exception //types are caught under Exception? I guess //What I really want to know, is will this ever //Fail? } catch(SystemException x){ //This code will never execute since all //exceptions are caught in the first catch? } 

Next, how does this trap work? If Exception is at the top, that is, every other level of exception of type 1 in Exception, or there are several types of types, for example, if Exception is the parent of ExceptionSomething, which is the parent of ExceptionSomethingElse?

:

Or, if we have a code like this:

 try{ ... } catch(SystemException x){ //If there is an exception that is not a SystemException //code in this block will not run, right (since this is //looking specifically for SystemExceptions)? } 
+4
source share
6 answers

Actually, it depends on your version and .NET configuration. In C ++ / CLI you can throw something ; It should not be an Exception . In 1.1 (IIRC), you could catch them with a catch block, for example:

 catch {...} 

This is not very useful - you cannot see what happened. In version 2.0 (IIRC), by default, such exceptions automatically end inside a subclass of RuntimeWrappedException . However, for compatibility, you can disable this. I beg you: not :)

+8
source

Yes, this works because all standard exceptions inherit from Exception .

Your code will work, but you will need to place a handler for Exception after all specialized exception types. (The first matching handler is running.)

Exceptions that are not inherited from Exception will not be caught because they are not the specified type. But .NET exception classes all inherit from this base class.

Some people do not consider this a good idea, but I usually catch Exception , unless I require special handling for a particular type of exception.

+4
source

To answer the second part of your question, an example hierarchy of exceptions from the .Net Framework:

 ArgumentNullException inherits from ArgumentException inherits from SystemException inherits from Exception 

You should try to handle the most specific case that you can.

 try{ //something } catch(ArgumentNullException ex){ //handle ArgumentNullException } catch(SystemException ex1) { //handle other kinds of SystemException if(IDontWantToHandleThisExceptionHere(ex1)) { throw;// not throw new Exception(ex1); } } 
+2
source

Yes, an exception is inherited from the Object class, and all exceptions are inherited from the Exception class.

The link above will show the hierarchy of all exceptions.

 System.Object System.Exception Microsoft.Build.BuildEngine.InternalLoggerException Microsoft.Build.BuildEngine.InvalidProjectFileException Microsoft.Build.BuildEngine.InvalidToolsetDefinitionException Microsoft.Build.BuildEngine.RemoteErrorException ... 

Some of these exceptions, such as the SystemException mentioned above, have additional exceptions inherited from them, but they are still inherited from the Exception class:

 System.Object System.Exception System.SystemException Microsoft.SqlServer.Server.InvalidUdtException System.AccessViolationException System.Activities.ValidationException System.AppDomainUnloadedException System.ArgumentException System.ArithmeticException ... 
+1
source

Last, exceptions can inherit from the Exception base class or any other class that inherits the base class.

For example: SqlException inherits from DbException , which inherits from ExternalException , which inherits from SystemException , which finally inherits from Exception .

+1
source

Yes, all exceptions are inherited from the exception.

And how inheritance works, you can have several levels of inheritance

 MyAppException : Exception { } MyAppFileNotFoundException : MyAppException { } 

This is usually used for different behaviors with different types of exceptions.

 try { openfile('thisFileDoesNotExist.txt'); } catch (MyAppFileNotFoundException ex) { //warn the user the file does not exist } catch (Exception ex) { //warn the user an unknown error occurred, log the error, etc } 
+1
source

Source: https://habr.com/ru/post/1348150/


All Articles