The role of Java exceptions

Source: Internet
Author: User

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

  1. int Login (string username, string password);

Then write an if else in the main program to determine the different processes.

Java code
  1.   int  logon = usermanager.login (xx,xx);;   
  2. if (Logon = =0); {  
  3. ...
  4. }  else   if   (logon ==  1
  5. ...  
  6. }  else   if   (logon == 2
  7. ..}

This is process-oriented programming logic, not object-oriented programming logic.

It should be written like this:

Java code
  1. User Login (string username, string password); throws  usernotfoundexception, passwordnotmatchexception;

The main program writes like this:

Java code
  1. Try  {  
  2. Usermanager.login (XX,XX);
  3. ....
  4. The main event stream code after the user logs in
  5. } catch (usernotfoundexception e) {
  6. ...
  7. The user name does not have event handling, such as generating a page that prompts the user to register
  8. } catch (passwordnotmatchexception e) {
  9. ....
  10. Event handling with incorrect password, e.g. forward to re-login page
  11. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.