Throw the exception directly with the throw, after the class and method with the throws

Source: Internet
Author: User
Tags throw exception
Throw is used to throw an exception directly after classes and methods are throws.
Throws is placed behind the class and method, and then you want to call the class and method, throw the class or method exception directly.
    Throw is a single statement throws an exception!
Throw is throwing a
Although the two seem to have only one s difference, but the effect is completely different/////java handling unusual way///////////////////////////////in Java code, if an exception occurs, the JVM throws an exception object that causes the program code to break.

The JVM is doing this by creating an exception object and then throwing it, for example: int i= 1; int j = 0; int res = 0; res = i/j;//except 0 error System.out.println (res); These 5 lines of code run to the fourth sentence will be interrupted because the JVM throws an exception////throw action/////////////////////////////////////////manually throws an exception but sometimes some errors are not mistaken in the JVM, such as int
Age = 0;
Age =-100;
System.out.println (age);
The normal shape of the variable assignment, but in our eyes seems to be abnormal, whose age will be negative.
So we need to throw ourselves an exception manually, which is the role of throw int age = 0;
Age =-100;

if (age<0) {Exception e = new Exception ();//Create exception object throw e;//throw Exception} System.out.println (age); Throws action///////////////////////////////////declaration method may avoid exceptions are thrown out, it is necessary to do processing, so there are try-catch in Java but sometimes a method produces an exception, But do not know how to deal with it, then put it regardless, when the exception is thrown will interrupt the method, and the exception is thrown to the caller of this method. This is a bit like a problem that the subordinates can't handle. This is called avoidance exceptions but this makes calling this method risky because no one knows when this method throws an exception to the caller, so when you define the method, It is necessary to use throws in the method header to declare the exception void fun () throws Ioexception,sqlexception {...} that this method may evade, which means that the fun method may throw two exceptions. Then you'll be prepared to call fun, such as try {Fun ();} catch (IOException e) {}catch (sqlexceptIon e) {} 

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.