Will you never catch an exception or make an exception that will not be caught?

I was dealing with instances in which I would throw / reconstruct an exception, knowing that the code surrounding it would catch the specific exception. But is there ever you want to throw an exception, knowing that they will not catch it?

Or at least NOT catch the exception?

Exceptions immediately pause the application if they are not handled correctly? So I think I'm asking if you want to ever intentionally let your application die?

+3
source share
14 answers

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

, , , , - . :

  • ? ? - , .

  • ? ; . , .

  • , ?. , , , .

  • . , , , . , , , - , , try-finally, .

+14

, :

, , .

, , - . , , , . , , , , .

:

, , .

, try... finally, , . . , , , (, catch (Exception ex) #), . , .NET 2.0 , , . , , , .

+8

, , , . , -, , , - . "" , , , , - . .

, , , . , - - , , . (, , ).

, : " ?"

+5

. , Java:

try {
  String myString = new String(byteArray, "UTF-8");
} catch (UnsupportedEncodingException e) {
  // Platform doesn't support UTF-8?  What is this, 1991?
  throw new RuntimeExceptione(e);
}

, . , , String - , . , Charset.forName():)

+4

... "", "" " ". - , - . . ... , , , , .

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

, , , ... , , .

- "". , , , . , Windows Forms. Windows Forms .

Windows. , , , , .

, . : " , , . , , - ". : ", , , , , ".

+2

- , , ?

, , , . , , , .

, , , , , .

?

/ , . , , , , - .

+1

. - , .

"/" , , . , , , .

B C ( )

B /

A .

, B , B , C . .

, , . , . , , . , " " - ", , , .

, , , , .

, amok .

EDIT

, , . , . , api , . ( , ", "... - , , .)

+1

-, , .

, . , . A NullReferenceException " ". , , , , . , , , .

, . , A , "" - , .

, - , : finally try ( , ). ? , . , - . . . , .

, , - , , , . , , Windows , , . , , .

-, , , ? , . , "", : , . , - , . A . . , , , , .

, Microsoft Enterprise Library: , . catch (Exception x), , ; - finally . .

+1

, - , , , API ( ) , .

, , Java. , . . , , , IllegalArgumentException.

, , , . .

0

, " ". -, - , - .

, , . . , , ( ).

0

, . , , , , .

. , , - . , .

, , , , ( , , IMO, - ).

API Framework, , , .

0

, , /, "Ur dO1n it WrOnG!!!!".

, "". , , - , .

0

, - , .

- , .

,

, , , . , , , .

, , , , , .

0

, , , . , , , .

( ++, #, , , ), , , , , , , . , C .

, ( , ), , . , , , .

, . , , , , , , , . , , - , , , catch " , , , . .

0

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


All Articles