Handling Exceptions in Java throw and throws

Source: Internet
Author: User
Tags try catch

1. Let's start by understanding what is an anomaly.

An exception prevents an issue in which the current method or scope continues to execute.

2. Handling Exceptions

When it comes to handling exceptions, of course we think of Try Catch finally

In Java we will have a higher understanding of the handling of exceptions we will learn to deal with exceptions like throw throws.

3.throw Keyword: statement throws an exception throws keyword: declaring an exception (method throws an exception)

01.throw is a statement that throws an exception.

Syntax: Throw (Exception object);
Throw e;

Throws is a declaration that the method might throw an exception.   (used when declaring a method, indicating that the method might throw an exception) The caller must make a deal (catch or continue throwing)

Syntax: [(modifier)] (return value type) (method name) ([parameter list]) [Throws (Exception Class)]{...}
public void DoA (int a) throws exception1,exception3{...}

03.throws can be used alone, but throw can not, throw either with try-catch-finally statement supporting use, or with throws supporting use . However, throws can be used alone and then caught by the method that handles the exception.

The 04.throw statement is used in the body of the method, indicating that an exception is thrown and processed by the statement in the method body
The throws statement is used after the method declaration, indicating that the exception is thrown, processed by the statement in the previous-level method that called the method, and must be processed (capture or continue declaration)

05.throws is basically declaring that this method throws this type of exception so that other places call it knowing to catch the exception so that the reminder must be processed. Otherwise, the compilation will not pass.
Throw is a specific action that throws an exception, so it is throwing an exception instance.

eg

 Public voidDothing (intAintbthrowsException1,exception3 {Try{                 //......           }Catch(Exception1 e) {Throwe; }Catch(Exception2 e) {System.out.println ("Print your own tips, don't throw"); }           if(a!=b)Throw NewException3 ("Custom Exception");}

3 exceptions can be generated in a code block (Exception1,exception2,exception3).
If a Exception1 exception is generated, it is then thrown after the capture and processed by the caller of the method.
If a Exception2 exception is generated, the method processes itself (that is, printing out a string: self-printing hints, not thrown). So the method will not throw out the Exception2 exception, void Dothing () throws Exception1,exception3 inside the Exception2 also do not have to write (of course, you do not write the error), throws is to declare the error that might be thrown, and Exception2 does not make a throw operation.
The Exception3 exception is a logic error in the method, the programmer has done their own processing, in the case of logic error thrown exception Exception3, the method's caller will also handle this exception.

Handling Exceptions in Java throw and throws

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.