Java exception throws

Source: Internet
Author: User

In Java, a programmer must throw a possible exception when the defined method may produce an exception.

Two ways of throwing exceptions, one is to use a throw statement inside a method, and the other is to use the throws keyword in the method definition

1.throw statements

In Java, the throw statement is used to throw an exception. The specific type of the exception can be defined by the programmer itself, or it can be a Java system exception type.

Remember, an exception is an object of an exception class, and when an instance of an exception class is available, you can use the throw statement to throw a

Syntax rules:

Throw Instanceexception Exception Object

2.throws keywords

To indicate that some code in your defined method may produce an exception, use the keyword throws when defining the method with the name of the exception thrown by the method.

Syntax rules:

..... Method Name () throws Exception class

In many cases we only define a method that throws an exception, but not to catch or handle that exception, which can be thrown using throws. A more reasonable usage is to handle exceptions when calling this method.

For example:

public static dosomething () throws runtimeexcep{

...........

}

Java exception throws

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.