Exceptions for Java

Source: Internet
Author: User

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

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.