Some ideas on Java Try-catch, throw and throws

Source: Internet
Author: User
Tags getmessage throw exception

Previously written code, rarely used exceptions, and later found that this habit is wrong. An exception is also a message, not an error.

1: Write a simple class first:

 Package Com.exception.demo;  Public class Main {    publicstaticvoid  main (string[] args) {        new Main ();    }      Public void Methodtry () {    }    publicvoid  Methodthrow () {    }}

The initial environment is such a simple answer.

2: The following method Methodtry Plus method body:

     Public Static void Main (string[] args) {        new  main ();        Main.methodtry ();    }      Public void Methodtry () {        int a=10;         int b=0;         int c=a/b;    }

This method will be written when you start to learn the code, throwing an exception:

  

The console clearly tells us that the divisor cannot be 0. But what if we want to get the exception ourselves and do something about it? For example, if there is a problem with this method, I will make an output.

     Public void Methodtry () {        try  {            int a=10;             int b=0;             int c=a/b;            System.out.println (c);         Catch (Exception e) {            System.out.println ("This method body has a problem:" +e.getmessage ());}            }

This is the time to use the Try-catch, manually catch the exception, and then do the actions we need. After all, there are many kinds of anomalies, and not all of them are what we don't need. For example, for the user login, login successful login failed two results, login failure is divided into duplicate login, account password mismatch and so on. We can write all these failures into exception. When it succeeds, it returns, and throws an exception when it fails, which is much simpler than writing a lot of return values.

Then say Try-catch.

  

We have manually captured this exception. The code above tells us that when there is an exception in the Try-catch block, the code after the exception is not executed. What good is Try-catch? Roll back.

3:throw

     Public Static void Main (string[] args) {        new  main ();        Main.methodthrow ();    }      Public void Methodthrow () {        thrownew Exception ("There is an exception here");    }

In fact, when I throw a simple exception, throw new Exception () here will be an error, now look at an error message:

  

The display lets us choose whether it is throws or Try-catch.

What do we mean by the throw we've written? In fact, with a/0 is a meaning, are thrown an exception, but one is the JDK has been defined by the exception, the divisor cannot be 0. One is the exception that we manually throw.

Try it with Try-catch first.

  

     Public void Methodthrow () {        try  {            thrownew Exception ("There is an exception here");         Catch (Exception e) {            System.out.println ("Methodthrow:" +e.getmessage ());        }    }

  

The point is that after you manually throw an exception, we're going to handle it in the catch and write our department logic code in the catch.

4:throws

We chose Try-catch and now choose throws.

     Public Static void throws Exception {        new  Main ();        Main.methodthrow ();    }      Public void throws Exception {        thrownew Exception ("There is an exception here");    }

Method Methodthrow throws exception, his parent class faces two situations, either Try-catch or throws the exception. This situation is the same as the problem encountered by manually throwing exceptions in Methodthrow.

It seems that you can understand this:

Throw is a manual throw exception, and dividend cannot be a 0 array subscript out of bounds, such as exceptions, are exceptions.

Try-catch is to manually catch an exception in a catch and then do something about it. For example, output exception information, print error log and so on.

Throws is to throw an exception to the superior, my method Methodthrow has an exception, but in this method I do not deal with, let the superior to deal with it. And ran to the main function. For the main function, you can throws the system for processing, or you can handle the exception yourself. Main.methodthrow () and a/0, throw new Execption () are no different, there are anomalies.

Can actually write the whole:

     Public Static void Main (string[] args) {        new  main ();         Try {            main.methodthrow ();         Catch (Exception e) {            System.out.println (e.getmessage ());        }    }      Public void throws Exception {        thrownew Exception ("There is an exception here");    }

The exception in the method, which is then processed in the main function.

Some ideas on Java Try-catch, throw and throws

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.