Best way to deal with a typical exception to the premise?

Which of the following ways to work with this precondition is more desirable and what are the more important consequences?

1:

If Not Exists(File) Then 
    ThrowException
    Exit
End If
File.Open
...work on file...

2:

If Exists(File) Then 
    File.Open 
    ....work on file...
Else
    ThrowException
    Exit
End 

Note. Checking for a file is just an example of a HANDLE prerequisite. Clearly, there is a good argument for checking for files themselves throwing their own exceptions up.

+3
source share
6 answers

I prefer the first option, so the best documents are that there are preconditions

+4
source

, . . , , , .

+2

. , 1. , , .

+1

. , ; , , if/else - , .

, , , , .
, , ; - .

.


. : , , , , , .
, , File.Open, , - , .

+1

. open(), open(). , . , :

  • IO- (, open, MySQL ). , , IO.
  • (, , , - UnableToAuthenticateException)

, . , , ifs. , , .

+1

- , , , : , , .

, , ; , djna : .

. , , , .

+1

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


All Articles