Miss Wang. Exception (vi): Throw exception: Throw exception with throw

Source: Internet
Author: User

Throw exception: Throw exception with throw
Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals:

1, master using throw throw exception

2. Summarize the two ways of exception handling in Java

First, the use of the situation

When an error occurs, the system automatically throws an exception, which is thrown by the throws declaration we used in the previous section, and in addition, Java allows programmers to throw exceptions themselves. Throws an exception by itself and completes with the throw statement.

So when does the programmer throw an exception by itself? In general, the programmer throws the exception, is generated by the business, such as gender and not "male" or "female", the system can not judge the abnormal, can only be determined by the programmer to throw.

Second, the grammar
1 throwExceptionInstance
Iii. example (i) write throw exception
123456789101112 public class Person {    private String name;    private int age;    private String sex;    public void setSex(String sex) throws Exception {        if("男".equals(sex)||"女".equals(sex)){            this.sex = sex;        }else {            throw new Exception("性别必须是:男或女");        }    }}
(ii) Catch throw exception
12345678910 public class Test {   &n bsp;  public static void main (string[] args) {          person person = new person ( );          Try {              Person.setsex ( "male" );          } catch (Exception e) {               e.printstacktrace ();          }      } }

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/7F/B5/wKiom1cprTvyG5pGAAAkLwtkQpo136.png " data_ue_src= "E:\My knowledge\temp\d3f04ba2-5249-40ba-9cdc-bae2a0681f3c.png" >

Iv. the difference between throw and throws
    1. Use different: Throw is used to throw an exception in the program; throws is used to declare that an exception was thrown within the method

    2. Use different location: Throw is inside the method body and can be used as a separate statement; throws must follow the method parameter list and cannot be used alone

    3. Content is different: Throw throws an exception object, and can only be one; throws is followed by an exception class, and can be followed by multiple exception classes.

V. Throw and catch use simultaneously

In the first few sections, we have two ways to deal with exceptions.

    • The exception is caught and handled within the method where the exception occurred, and with try-catch-finallly, the caller of the method does not have to catch the exception again.

    • The method in the signature declares that the exception is thrown, and the exception is passed to the method call processing.

(i) Application scenarios

In practice, more complex processing is often required: When an exception occurs, it cannot be handled completely by a single method, and it must be handled by several methods to fully handle the exception. That is, in the current method in which the exception occurs, the program only partially processes the exception, and some processing requires the caller of the method to complete, so the exception should be thrown again.

(ii) examples
1234567891011121314151617 public class Cal {    public String division(String stra, String strb) throws Exception{        try {            int a = Integer.parseInt(stra);            int b = Integer.parseInt(strb);            int c = a / b;            return String.valueOf(c);        } catch (NumberFormatException e) {            System.err.println("数字格式异常:程序只能接受整数参数");            return "数字格式异常";        } catch (Exception e) {            throw new Exception("未知异常");        }finally{            System.out.println("感谢您使用本程序!");        }    }}
12345678910 public class Test6 {      public Static void Main (string[] args) {          cal cal = New Cal ();        

With Miss Wang Exception (vi): Throw exception: Throw exception with throw

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.