"Exceptions" in Java

Source: Internet
Author: User

Exception: A problem that occurs during Java Operation is an exception, and in addition to the exception exception that occurs in Java, there are error errors.

Exception: can be targeted processing; Error: Usually there will be no targeted processing, often a system-level problem, unable to deal with, can only fix code.

For example, given an array with a given cross-table gets the element,

Class demo{/* gets the element by a given corner mark on the given array. */int getelement (int[] Arr,int index) {/* It provides insufficient information to force. Want to be clearer and need to write for yourself. It's thrown out not to meet our requirements. Ready to throw yourself. */if (arr==null) {throw new NullPointerException ("The array the arr points to does not exist");} if (index<0 | | index>=arr.length) {//The condition is satisfied, the function cannot continue the operation. At this point, you must end the feature and inform the caller of the problem. This will require an exception to be resolved. How to use it? 1, create an exception object. Encapsulates some hint information (custom). 2, this object needs to be communicated to the caller. How to Tell? How do you pass this object to the caller? You can do this by using the keyword throw. Throw exception object;//3,throw is used within a function to throw an exception object and to end the function. throw new ArrayIndexOutOfBoundsException ("Wrong corner label," +index+ "index does not exist in the array");}
Custom Exceptions:

Exception in the top of the exception is not good, ready to define a meeting my program requirements of the problem name and the problem of information, in fact, the exception is to tell the problem, encapsulated into the object.

Lookup API Discovery:

Throwable

|--error

|--exception

Throwable is the parent class for exceptions and errors, custom exceptions are thrown and must be inherited throwable or throwable subclass, the object can be thrown by throw.

Declaration and capture:

Declaration: Identify the problem and report it to the caller.
If a compile-time exception is thrown inside a function through a throw, it must be declared by throws to be handled by the caller.

Capture: An exception-targeted statement is captured in Java.
Statement:
Try
{
The statement that needs to be instrumented.
}
catch (Exception class variable)//parameter.
{
The processing statement for the exception.
}
Finally
{
Statements that are bound to be executed.
}

What is the difference between throw and throws?

1,throw is used within a function.
Throws is used on functions.
2,thorw throws an exception object.
Throws is used to declare the exception class, after which the exception class can have multiple, separated by commas.

Class Demo{void Show () throws Exception{throw new Exception ();}} Class  {public static void main (string[] args) {System.out.println ("Hello world!");}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Exceptions" in Java

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.