When and why should I explicitly throw an exception

I'm just trying to understand my understanding of the exception mechanism. If something goes wrong when the application is executed, the runtime automatically throws an appropriate exception. If so, why should I sometimes check and explicitly throw a specific exception?
How to determine such cases (when an exception should be explicitly introduced into the code)?

I did not set any example here specifically, because I would like to get a general idea of ​​the method of eliminating exceptions.

+5
source share
6 answers

, Fail Fast - , , , , "" ( / ).

, , , , , .

+6

, , , , , , , . , , , , null, <ArgumentNullException

+3

, , , , , ( ) , .

", ", , . , SaveFile(), , , , , , ... SaveFile() - , , .

+3

, ,

  • / , - ( foos n , )

  • ,       , ,    (      , ,    ( , ,    ..), ,    .

  •      , ..          ,    / ..

+2

, - . ( "x null!" - , ). , - , . . , (java,.net), , ( , , ,...), , .

: , , . NPE - , .

+2

( , void, int, string, car ..) , T, Exception, "- ". , , ( , SQLException) . ( ) .

0
source

All Articles