Java Exception One

Source: Internet
Author: User

Exception: is the abnormal condition that occurs when the program is "running."
Exception Origin: The problem is also a specific thing in real life, can also be described in the form of Java classes. and encapsulated into objects.
In fact, Java is a description of the abnormal situation after the object.

The problem is divided into two kinds: one is a serious problem, the other is a non-serious problem.
For serious, Java is described by the error class. --The error generally does not write the targeted code to handle it.
For non-critical, Java is described by the exception class. --for exception can be processed using a targeted approach.

Both error and exception have some common content. For example: abnormal information, causes of the cause and so on.


The Throwable has two primary subclasses:
|--error
|--exception

Common method operations on caught exception objects are:
String getMessage (): Get exception information
String toString (): Get exception Name: Exception information
void Printstacktrace (): Prints the exception name, exception information, where the exception occurred.


Declare the exception on the function.
Easy to improve security, let the call out for processing. Failed to process compilation.

Handling of Exceptions:
1, when declaring an exception, it is recommended to declare a specific exception. This can be handled more concretely.
2, the other side declares a few exceptions, corresponding to a few catch blocks, do not define redundant catch blocks;
If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed at the bottom.

It is recommended that you define the specific handling in catch when you are doing catch processing.
Don't simply define a sentence e.printstacktrace (),

Do not simply write an output statement.


Class Arithmetic{int method (int a,int b) throws arithmeticexception,arrayindexoutofboundsexception{int[] Arr=new Int[a  ]; Although there are two possible exceptions, the program terminates when the first exception is encountered at run time, and subsequent programs are no longer executed. System.out.println (Arr[4]); return a/b;}} Class Exceptionclassdemo{public static void Main (String args[])//throws exception//A feature that could cause problems by declaring the feature with a throws keyword. {Arithmetic ar=new arithmetic (); Try{int Result=ar.method (4,0); SYSTEM.OUT.PRINTLN ("Result:" +result);} catch (ArithmeticException e)//exception e = new ArithmeticException (); {System.out.println ("divisor cannot be zero! ");  System.out.println (E.getmessage ());//by Zero System.out.println (e.tostring ());               Exception Name: Exception information E.printstacktrace ();                                   Prints the exception name, exception information, where the exception occurred. In fact, the JVM default exception handling mechanism is to call the Printstacktrace method//Print the exception of the stack tracking information. }catch (arrayindexoutofboundsexception e) {System.out.println (e.tostring ()); SYSTEM.OUT.PRINTLN ("Array Out of Bounds");} System.out.println ("Over");}}


Java Exception one

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.