Throw and throws, throwthrows

Source: Internet
Author: User

Throw and throws, throwthrows

UncheckException handling

Class User {private int age; public void setAge (int age) {if (age <0) {// generate the exception object RuntimeException e = new RuntimeException ("Age cannot be negative"); throw e; // terminate the running and throw the exception object} this. age = age ;}}
class Test{      public static void main(String args[]){           User u = new User();           u.setAge(-20);      }}

 

Exceptions that cannot be recognized by JVM. For example, if the age is negative, there is no syntax error but they do not conform to the actual situation. We need to manually identify the exception, generate an exception object, and throw an exception through throw. After the JVM obtains the exception object, the code is terminated.

 

CheckException handling

Class User {private int age; public void setAge (int age) {if (age <0) {Exception e = new Exception ("age cannot be negative"); throw e ;} this. age = age ;}}

  

Capture: Use try... Catch... Finally

Declaration: Use throws

When the setAge method may generate a checkException, such as an Exception type Exception, add an Exception object such as throws declaration Exception after the function. After an Exception is generated, it is handled by the place where the setAge method is called.

Class User {private int age; public void setAge (int age) throws Exception {if (age <0) {Exception e = new Exception ("age cannot be negative "); throw e;} this. age = age ;}}

The following error is prompted when you compile Test. java again:

class Test{public static void main(String args[]){User u = new User();try{u.setAge(-20);}catch(Exception e){e.printStackTrace();}}}

When the setAge method in the User class declares the exception type through throws, the Test main function calls the setAge method to generate an exception and uses try... Catch.

 

Summary:

Throw: an exception that the JVM cannot judge. You can use thorw to Throw an exception by generating an exception object.

Throws function: it is used to declare that a function may produce exceptions. The function does not handle exceptions. The exception is handled when a function is called.

Reference: http://dev.yesky.com/61/8111561.shtml#top


In java, Where Are throws and throws used?

Throws can only appear after the Method

Public void method () throws Exception

Throw can be used in the code to actively throw an excetion. If throw occurs in the code, you must write throws xxx to the called method.
For example
Public void method () throws Exception {
Xxxx
Throw Exception ();
}

What is the difference between throws and throw?

Throw refers to a specific exception type thrown. Generally, throws is used to declare the exception information that a method (class) may throw at the declaration of a method (class), and throw is used to declare a specific exception information within the method (class. Throws usually do not need to display capture exceptions. The system can automatically throw all captured exception information to the upper-level method. throw requires the user to capture the relevant exceptions and then package them, finally, the encapsulated exception information is thrown. If an exception is thrown in the method and you do not want to handle it in the method body, you can use throws to declare the exception that may run when declaring the method. For example: public void go () throws SQLException {Connection conn = ds. getConnection ();...} originally ds. getConnection () may throw SQLException, but throws is used to declare the method name, therefore, the try/catch Block is not required in the method body to handle SQLException. The throws statement throws is used to indicate various "exceptions" that a member function may throw ". For most Exception subclasses, the Java compiler forces you to declare the "Exception" type thrown in a member function. If the "exception" type is Error or RuntimeException, or their subclass, this rule does not work because this copy; is not expected in the normal part of the program. If you want to explicitly throw a RuntimeException, you must use the throws statement to declare its type. In this way, the definition syntax of the member function is redefined: type method-name (arg-list) throws exception-list {}. Below is a program that throws an "exception ", but neither capture it nor declare it with throws. This will not pass during compilation. Class ThrowsDemo1 {static void procedure () {System. out. println ("inside procedure"); throw new IllegalAccessException ("demo");} public static void main (String args []) {procedure ();}} to compile this example, we need to declare that the member function procedure throws the IllegalAccessException and captures it in the member function main that calls it. The following is a correct example: class ThrowsDemo {static void procedure () throws IllegalAccessException {System. out. println ("inside procedure"); throw new IllegalAccessException ("demo");} public static void main (String args []) {try {procedure ();} catch (IllegalAccessException e) {System. out. println ("caught" + e) ;}} below is the output result: C: \ java ThrowsDemo inside procedure caught java. lang. illegalAccessExceptio ...... remaining full text>

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.