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