1. What is an exception?
First, the anomaly simulates "abnormal" events in the real world.
Second, Java uses "class" to simulate exceptions.
Third, a class is an object that can be created.
NullPointerException e = 0x1234;e is a reference type, and the memory address stored in E points to "object" in the heap. This object must be of type NullPointerException. This object represents a real exception event. NullPointerException is a class of exceptions.
"Robbery" is a class of exceptions. -----> class
"Zhang San was robbed" is an unusual event----> object
2. What is the function of abnormal mechanism?
The Java language provides us with a perfect exception handling mechanism,
The function is: After the exception event of the program, output detailed information for us,
Through this information, the programmer can do some processing to make the program
More robust. Code:
Public class exceptiontest01{public static void main (String[] args) {Int a = 10;int b = 0;int c = a/b; //arithmeticexception e = 0x2356;//The code above has an exception, "No processing", the following code will not execute and exit the JVM directly. System.out.println ("hello world!");}} /* The above program was compiled, but there was an exception at run time, indicating an exception event occurred. The JVM outputs the following information to the console: Essence: The event that an arithmetic exception occurred during program execution, the JVM created an object of type ArithmeticException for us. And this object contains detailed exception information, and the JVM outputs the information from this object to the console. exception in thread "Main" java.lang.ArithmeticException: / by zero at exceptiontest01.main (exceptiontest01.java:25) */ Exception handling makes the program more robust. Public class exceptiontest02{public static void main (String[] args) {Int a = 10;int b = 0;if (b!=0) {int c = a/b; System.out.println (a + "/" + b + "=" + c);} ELSE{SYSTEM.OUT.PRINTLN ("Divisor cannot be 0");}}}
This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661693
Exceptions for Java