1. Abnormal effect: improve the interactivity of the software .
For example, login examples:
(the correct return is 0, if the user can not find is-1, if the password is not correct, that is-2): These are required to write the login method and the person calling the method to agree, and if the exception, the caller can see the name of the exception to know how to deal with, also saves a variety of if else
- int Login (string username, string password);
Then write an if else in the main program to determine the different processes.
Java code
-   int logon = usermanager.login (xx,xx);;
- if (Logon = =0); {
- ...
- } else   if (logon == 1
- ...  
- } else   if (logon == 2
- ..}
This is process-oriented programming logic, not object-oriented programming logic.
It should be written like this:
Java code
- User Login (string username, string password); throws usernotfoundexception, passwordnotmatchexception;
The main program writes like this:
Java code
- Try {
- Usermanager.login (XX,XX);
- ....
- The main event stream code after the user logs in
- } catch (usernotfoundexception e) {
- ...
- The user name does not have event handling, such as generating a page that prompts the user to register
- } catch (passwordnotmatchexception e) {
- ....
- Event handling with incorrect password, e.g. forward to re-login page
- }
2.
Exceptions have a certain difference from common warnings. When an application exception occurs, it interrupts the normal instruction flow of the program being executed. That is, the code that follows the exception will not be executed correctly. The fallback operation of the database is even triggered. Exceptions are prerequisites for the proper operation of the application.
3,
Exception handling is conducive to the correct display of error messages and errors of the location, in order to carry out the next step of related operations, such as a Web program, the background error, the front desk or nothing is displayed or display a bunch of code error messages, not intuitive, this time you do some packaging processing, at the front desk at a glance, For example, insert data because the link database exception error, the front desk needs to give what reason instead of see the print station of those code errors of the user can not understand the information, exception handling function or a lot of, this is just one thing, you will understand the necessity
The role of Java exceptions