java-Exception Handling

Source: Internet
Author: User
Tags class definition throwable

2017-10-16 19:39:57

Exceptions are errors in the program, but not all errors are exceptions, and errors can sometimes be avoided.

For example, your code is missing a semicolon, then run out of the result is the hint is error java.lang.Error; If you use System.out.println (11/0), then you are because you use 0 to do the divisor, will throw The java.lang.ArithmeticException exception.

Exceptions occur for a number of reasons, usually including the following categories:

    • The user entered illegal data.
    • The file you want to open does not exist.
    • The connection is interrupted during network communication, or the JVM memory overflows.

One, the problem of the procedure: Throwable

~ Errors: Error, do not handle, this problem is generally more serious, such as memory overflow, need to be further resolved;

~ Exception: Exception

* Compile-time Exceptions: Exceptions that are not runtimeexception are required and must be handled if the compiler does not handle the pass. Common compilation exceptions are: IOException (stream transfer exception), SQLException (Database operation exception), and so on.

* Run-time Exceptions: RuntimeException exceptions, which are not detected by the compiler when the code is written, are not captured, but can be thrown by the programmer as needed. Common runtimeexception are: nullpointexception (null pointer exception), ClassCastException (type conversion exception), Indexoutofboundsexception (array out-of-bounds exception), and so on.

If there is a problem with the program, but the programmer does not make any processing, the JVM will eventually make the default processing.

That is, the name of the exception, the reason and the occurrence of the line number and other information output on the console.

The program is also closed.

Second, the solution

    • Try...catch...finally ...
try{      code that may be problematic,       as little as possible,      }catch (exception name variable 1) {      for problem handling,}catch (exception name variable 2) {      for problem handling;}finally{      Whether or not an exception will be executed, usually only when the file is closed, the network connection is used;}

* Generates an exception object and interrupts the currently executing code, throwing an exception object.

* Automatically find the "closest" exception match in the order in which the catch in the program is written, and once found, it is considered that the current exception has been controlled and no further searches are made. In addition, the catch must deal with the corresponding exception, otherwise it loses its meaning.

* If there is a match to execute the corresponding processing code, and then continue to execute the code after the try block, otherwise, the uncaught exception will be the default exception handling, processed by the JVM, the console output relevant information, and exit the program run.

* No situation will return to the wrong place to continue execution.

* Be clear about the anomalies as clear as possible, do not use only large exception to deal with

* Catch can also be used at this time (Exception 1 | Exception 2 | Exception name) to handle a variety of exceptions. However, the processing is consistent, and multiple exceptions must be a peer relationship.

* Common methods for Exceptions: E.getmessage (): Returns the message string of an exception

E.tostring (): Returns a simple description of the exception, "including the full path name of the class: Error name Time"

E.printstacktrace (): Gets the simple information description of the exception, equivalent to calling the ToString method, and also outputting the line number information

* Finally, if you exit the JVM, then finally, if there is a return, the finally statement will be executed and returned back after execution, and the return value in the original return path will not be changed.

    • Throws/throw

If a method does not catch a compile-time exception, the method must be declared using the throws keyword. The throws keyword is placed at the tail end of the method signature. Throws an exception in the method to the caller for processing.

You can also throw an exception with the Throw keyword, whether it's newly instantiated or just captured.

Import java.io.*;p ublic class classname{public  void deposit (double amount) throws RemoteException  {    // Method implementation    throw new RemoteException ();  }  remainder of class definition}

III. Custom Exceptions

In Java, you can customize exceptions. Here are some things to keep in mind when writing your own exception classes.

    • All exceptions must be subclasses of Throwable.
    • If you want to write a compile-time exception class, you need to inherit the Exception class.
    • If you want to write a run-time exception class, you need to inherit the RuntimeException class.
    • The construction method has two non-parameters, one parameter, which is mainly used to print the exception information to the control table.

public class MyException extends Exception



}

Iv. Considerations for exceptions in succession

    • When subclasses override parent class methods, subclasses must throw exceptions that are consistent with the parent class or subclasses of the parent exception (the father is bad, the subclass can be worse)
    • If the parent method does not throw an exception, the overridden method in the subclass cannot throw the exception and can be processed using a try block

java-Exception Handling

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.