About Exceptions in APCS JAVA

Source: Internet
Author: User

What is exceptions

First we need to know what is exceptions, defined as follows

Exceptions:an exception is an event that occurs during the execution of a program that disrupts the normal flow of Instru Ctions.

This is the official definition, please click here for details.

At the same time we can define exception as: An exception was an error condition that occurs during the execution of a Java progra M.

Two points in this sentence: 1,exception is an error (think of another error we will encounter in APCs-compile error)

2, occurs during the Exectution, which is the error that occurs during the run.

APCS examination to which different kinds of exceptions

There are many kinds of exceptions, and each kind of Exception has its own name made up of "cause of error +exception". The following classes will be cover to the APCs.

1,arithmeticexceptions

What will happen: thrown when a exceptional arithmetic condition has occurred. For example, a integer "divide by zero"

1  Public classTest2 {   3      Public Static voidMain (String args[]) {4         intx = 0;5         inty = 6;6System.out.println ("y/x =" + y/x); 6/0 would cause an arithmeticexception here7     }8}

Run the result as

Java.lang.ArithmeticException:/By zero
At Test.main (test.java:6)

2,nullpointerexception

What happens: Thrown when an application attempts to use in null a cases where an object is required.

In short, this object does not "exist" when you want to invoke the public method of an object or use public instance variable.

1  Public classTest2 {   3     Private intx;4      Public intGetX () {5         returnx;6     }7     8      Public Static voidMain (String args[]) {9Test T =NewTest ();Tent =NULL; OneT.getx ();//May cause NullPointerException A     } -}

Run the result as

Java.lang.NullPointerException
At Test.main (test.java:11)

3,arrayindexoutofexception

What happens: Thrown when an application attempts to use in null a cases where an object is required.

About Exceptions in APCS 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.