Describe the simple principles and applications of the exception handling mechanism in Java, and what is the difference between error and exception?

Source: Internet
Author: User
Tags getmessage throwable

1: Please describe the simple principle and application of exception handling mechanism in Java, and explain the difference between error and exception.

Every time an exception is generated, if there is no program to handle, then the program will interrupt the phenomenon, then, in fact, once an exception is generated, the JVM will throw an exception class instantiation object, if you use the Try statement to capture, you can do exception handling, if not, is handed to the JVM for processing, and when the try statement catches an exception, it matches the exception type in the catch, and if the match succeeds, the catch statement is used for processing.

Application: A simple application is to join the Try...catch in all throws keywords
If you follow a standard practice, try, catch, finally, throw, THORWS keywords should be used together.

When a Java program violates the semantic rules of Java, the Java Virtual machine represents the error that occurred as an exception. The violation of the semantic rules consists of 2 cases. One is the semantic check built into the Java class library. For example, an array subscript is thrown out of bounds, and a nullpointerexception is raised when an object that accesses null indexoutofboundsexception. Another scenario is that Java allows programmers to extend this semantic check, and programmers can create their own exceptions and freely choose when to throw an exception with the Throw keyword. All exceptions are java.lang.Thowable subclasses.
Eg: the first kind
Class test{
public int devide (int x, int y) throws exception{//
int result = x/y;
return result;
}
}
Class testexception{
public static void Main (string[] args) {
try{
New Test (). Devide (3,0);
}catch (Exception e) {
System.out.println (E.getmessage ())//Although there are no other functions in exception except constructor,
But it inherits the Throwable class.
}
SYSTEM.OUT.PRINTLN ("program running Here");
}
}
When writing test class does not know whether it throws an exception, you can add throws Exception after Devide the methods method ... In TestException, the thrown exception must be handled.
Eg: the second type
If you define an exception class yourself, you must inherit exception, which is its subclass.
Class test{
public int devide (int x, int y) throws exception{//
if (Y < 0)
Throw Devidemyminusexception ("devide is" + y);
int result = x/y;
return result;
}
}
Class Devidemyminusexception extends exception{
Public devidemyminusexception (String msg) {
Super (MSG); Invokes the construtor of the parent class (Exception), the main function getmessage () outputs the custom exception: "Devide is ..."
}
}
eg. the third type
You can throw multiple exception classes.
Class test{
public int devide (int x, int y) throws arithmeticexception,devidemyminusexception{//
if (Y < 0)
throw new Devidemyminusexception ("Devide is" + y);
int result = x/y;
return result;
}
}
Class Devidemyminusexception extends exception{
Public devidemyminusexception (String msg) {
Super (MSG);
}
}

Class testexception{
public static void Main (string[] args) {
try{
New Test (). Devide (3,-1);
}catch (ArithmeticException e) {
SYSTEM.OUT.PRINTLN ("program running into ArithmeticException");
E.printstacktrace ();
}catch (Devidemyminusexception e) {
SYSTEM.OUT.PRINTLN ("program running into Devidemyminusexception");
E.printstacktrace ();
}catch (Exception e) {
System.out.println (E.getmessage)
) finally{
SYSTEM.OUT.PRINTLN ("finally Running");//No matter what happens to the program, finally code blocks are executed.
}


SYSTEM.OUT.PRINTLN ("program running Here");
}
}
1. The program is matched according to the type of exception. Automatically enter the appropriate catch statement. Exception should be placed after other exception statements, because they all inherit exception, the other exception to put in the back, there is no meaning.
2.try {} There is a return statement, so the code in the finally {} after this try will not be executed, when it is executed, before or after it? is executed and executed before return.
3. When does the finally code not execute? When System.exit (0) appears, it does not execute and the program exits.

The difference between error and exception:

The error and exception in Java are all coming to throwable this class, is his subclass,

Error: Is the system of errors, programmers can not change, processing, in the process of compiling errors. Only by modifying the program can the error be corrected.

Exception: The error that is caught while the program is running is an exception that can be handled.

Syntax is: try{}

catch (Exception name) {}

Finally

{Cleanup of resources: for example, close open files: delete temporary files; ....}

Exception: It is also divided into two types: runtimeexception (which can often occur without catch) and other Exception (which must be catch, so that the program can continue, like a method with throw IOException).

Related Article

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.