Throw and throws usage

Source: Internet
Author: User
Tags throw exception
1.java Handling Exception modeIn Java code, if an exception occurs, the JVM throws an exception object that causes the program code to break, and 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
the role of 2.throwThrow exceptions manually but sometimes Some errors are not mistaken in the JVMFor example
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 manually throw the exception, 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);
the role of 3.throwsThe exception that declares the method may be evasive is thrown, have 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 aside, when there is an exception 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, You need to use throws in the method header to declare exceptions that this method might avoid
void Fun () throws Ioexception,sqlexception
{
...
}
This means that the fun method may throw out two exceptions, and you'll be prepared to call fun, such as
Try
{
fun ();
} catch (IOException e)
{
}catch (SQLException e)
{
}
4. Method Principle

exceptions thrown automatically by the system all system-defined compile and run exceptions can be automatically thrown by the system, called Standard exceptions , and Java strongly requires the application to perform complete exception handling, give user-friendly prompts, or fix the program to continue.

Statement, custom exceptions and application-specific exceptions that are thrown by the exception user program must be defined by using the throws and throw statements to define the thrown exception.

throw is a statement that throws an exception.          Syntax: Throw (Exception object); Throw e;

throws is a declaration that a method might throw an exception .             (When declaring a method, it means that the method may want to throw an exception) syntax: [(modifier)] (return value type) (method name) ([parameter list]) [Throws (Exception Class)]{...} public void DoA (int a) throws exception1,exception3{...} 5. For example:

Throws E1,e2,e3 simply tells the program that this method may throw these exceptions, and that the method's caller may want to handle the exceptions that may be generated by the function body. Throw is clear about this place to throw this exception.

void DoA (int a) throws exception1,exception3{
           try{...
           } catch (Exception1 e) {
              throw e;
           } catch (Exception2 e) {
              System.out.println (") was wrong. ");
           }
           if (a!=b)
              throw new  Exception3 ("Custom Exception");
}

3 exceptions may be generated in the code block (Exception1,exception2,exception3). If a Exception1 exception is generated, it is caught and then thrown, which is handled by the caller of the method. If a Exception2 exception is generated, the method handles itself (that is, the SYSTEM.OUT.PRINTLN ("error)." ");)。 Therefore, the method will not throw out the Exception2 exception, void DoA () throws Exception1,exception3 inside the Exception2 also do not have to write. The Exception3 exception is a logical error in the method, which the programmer handles and throws an exception Exception3 in the case of a logical error, and the caller of the method handles the exception.

The throw statement is used in the method body, which indicates that an exception is thrown and is handled by the statement in the method body. The throws statement is used after the method declaration to indicate that the exception is thrown again, which is handled by the caller of the method.

Throws mainly declares that this method throws this type of exception so that its caller knows to catch the exception. Throw is a specific action to throw an exception, so it throws an exception instance.

Throws shows that you have that possibility, inclination. Throw, that's the way you turn that tendency into reality. 6. Summary 1, throws appears in the method function header, and throw appears in the function body.

2. Throws represents a possibility of an exception that does not necessarily occur; throw throws an exception, and execution throw must throw an exception.
3, both are negative ways to deal with the exception (the negative here is not to say that this way is not good), just throw or may throw an exception, but not by the function to deal with the exception, the real handling of the exception by the function of the upper call processing.

In this paper, when sorting out the knowledge points, we refer to other materials, aiming to learn from each other to improve the hope Haihan.

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.