How should I use exceptions?

Pretty new to C #, and I would like to know how to use exceptions? I do not mean the level of mechanics, but the level of good practice.

I will use, for example, my calculator, which tokens and converts to RPN and solves the problems specified in RPN.

During the tokenization step, there are various invalid inputs, for example "7.7.8" or "^ & ##", should I have separate exceptions for unknown characters and invalid numbers? Is it wrong to say that there is one exception and then a method containing the type of error that should be provided to the user?

I really could not find a lot of material about this, so I thought that I would ask people with more experience than me.

----- Edit:

Thank you all for your wonderful answers, I found out ton today :) about my question and even about what really is.

+3
source share
8 answers

Think of your lexer in terms of prerequisites and postconditions. Here are two ways to develop lexer:

  • A lexer is a device that accepts a valid input line and displays lexical analysis. The input line should be well formed; if this is not the case, then the lexical analyzer fails with an exception.

  • A lexer is a device that takes an input string and performs lexical analysis. Lexical analysis includes identification and analysis of error conditions.

, , , , , ? , , , lexer ?

. .

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

+15

. , . , , , . , , , , , , .

, , "" , , / , ..

+3

, . , , ?

, , , "catch".

, , , , .

+2

, , , : promises - , . Parse vs TryParse. , Parse , , (, ). TryParse promises , , , . , , .

: , . Eric Lippert Vexing Exceptions , .

+1

KISS. , . , .

  • InvalidArgument , , .
  • , , .

, , /.

"", , , .

0

, , , , catch (, , ).

, , . , , . , -, . .

: , , , promises, . , promises - , API- .

0

All Articles