What is the point of throwing an exception?

I am writing a C # login class, and I try to be careful about throwing exceptions for null passwords, incompatible password characters, etc. What suddenly happened to me was: what should I do with this exception? What are they for? Regardless of whether I handle the exception or not, the application will still fail. Is an exception for other developers, clients !?

+4
source share
11 answers

Exceptions are used to provide detailed information about the cause of a particular failure. If you simply skip the code yourself, you will miss the opportunity to provide more details about the actual cause of the failure.

Your users should not see the information you add to your exclusions. Instead, consider adding a global exception handler that catches your detailed exceptions, logs the information, and then gives you a welcome error message.

Exceptions provide not only a detailed message in your logs about why the failure occurred (i.e. the password was zero in your example), but also call stack information that indicates the call chain that led to the exception. In the login form, this is less important. However, in a multi-threaded asynchronous client / server application, this can be critical.

This article contains many good recommendations: http://msdn.microsoft.com/en-us/library/ms229005.aspx

+6
source

If you create an exception, you should not do anything with it.

Exceptions are your way of letting your consumers know that something went wrong, that you cannot recover normally. You give them the opportunity to either fix the problem, register the error, or pass the exception to the chain until something useful is done with it.

+6
source

To signal to the next level of abstraction that there is a problem, and change your logical flow to account for the problem.

+3
source

An exception in itself prevents big problems in the future. If you were just silent when you knew that something happened that was not there, the program that calls your code may suggest that the user file was saved when it really was not, which obviously could be worse than if so it could say "I could not save the file."

The message provided to the exception is for other developers. If the program crashes during development, the developer should be able to see the stack trace and more easily understand why something happened, which did not happen. Ideally, you will be able to log errors so that developers can see them even during the production process.

+3
source

Exceptions usually indicate that a method contract has been violated. The client of the method takes care of the exception and must handle them accordingly. When a contract is violated, the method itself usually cannot recover and cannot produce meaningful results. An exception indicates that no meaningful results are expected.

+2
source

In short, it must be pointed out that something that was not supposed to happen really happened.

+1
source

Exceptions are a way for the code to notify callers of some failure. The calling code can do whatever it wants with it, for example, display an error message, suppress an exception and correctly deform, etc.

0
source

An elegant way to show what they (Clients) did poorly.

0
source

So. You wrote a great program. this program has a possible point of failure. if for any reason part of the program crashes, you can still continue the rest of the program and either pay attention to the refusal, register it or just continue.

I am going to use the pseudocode syntax, but you should follow it:

var pw=$_POST['pw']; var un=$_POST['un']; try{ $sql="select lastlogin,access from users where un=q(/'$un'/) and pw= q(/'md5($pw)'/)"; $user=$db->getRow($sql); if(!$user) { //they don't exist }else{ //process their login } }catch(Exception $e){ //we has a Database error. either my query s really screwed up or the DB is down. let log it and exit this stream; service $mylogger->log("Error while logging in using module $MODULENAME$ ".implode("<br/>",(array)$e)); exit; } 
0
source

If your function will throw an exception, if it cannot enter the system, then the code that calls your function may suggest that if your function returns, it will enter the system. This will ease the amount of work that the login successed script needs to execute to process the code, instead requiring more work in the failed login script. If the code is sometimes used in cases where the failure is half-expected (for example, try logging in with one set of credentials, if that doesn’t work, try another set) and sometimes used when the failure is unexpected and irreparable, it can be useful either have a boolean flag "throw on error", or have separate methods "Login" and "TryLogin".

0
source

The nature of the exceptions is that they should be explicitly ignored. Suppose you have this function:

 bool authenticate ( String username, String password ) { if ( invalid_password(password) ) { return (false); } // ... perform authentication ... } 

Now think that this is part of a server, and that the server is running in a very privileged context. If the calling code (the one that performs the authorization) has some error in it, it may inadvertently allow users to perform actions that are usually not allowed.

I would write the following function:

 void authenticate ( String username, String password ) { if ( invalid_password(password) ) { throw new LoginFailed(); } // ... perform authentication ... } 

Please note that this is a purely protective approach to programming, and this is my preference in this context. Thus, a login error will most likely prevent the operation from continuing.

-3
source

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


All Articles