The 12th chapter of "Thinkinginjava"-Exception handling

Source: Internet
Author: User

The basic idea of Java is that poorly structured code does not run

Exception handling can maintain the robustness of the program and the system. 12.1 Basic Anomalies

An exception situation is a problem that prevents the current method or domain from continuing. The difference between an exception and a common problem is that a common problem can get enough information to handle the error in the current environment, and the exception situation cannot get the necessary information to solve the problem in the current environment, so it cannot go on. All you can do is jump out of the current environment and give the problem to a previous level of environment, which is what happens when you throw an exception . what happens when an exception is thrown. First, Java uses new to create an exception object on the heap. The current execution path is then terminated and a reference to the exception object is ejected from the current environment at the same time, the exception handling mechanism takes over the program and starts looking for an appropriate place to continue executing the program.

The appropriate place to refer to is the exception handler, whose task is to recover the program from the error state so that the program either runs in a different way or continues to run.

A simple example of throwing an exception: an object reference T, which may not have been initialized when it is passed, when invoked, it is checked before it calls its method, by which an object representing the error message can be created and "thrown" from the current environment, which propagates the error message to the "larger" environment. This process is called " throw an exception ", as follows:

if (t = = null) {
 throw new NullPointerException ();
}

This throws an exception so that you don't have to worry about it in the current environment and it will be dealt with elsewhere. 12.1.2 Exception Parameters

Like other objects in Java, we create exception objects on the heap with new, along with the allocation of storage space and the invocation of constructors. all standard exception classes have two constructors : one is the default construct, and the other is the receive string as a parameter so that the relevant information can be placed in the constructor of the exception object: throw new NullPointerException ("t = null").

You can throw any type of throwable object, which is the root class of the exception type. 12.2 Catching exceptions 12.2.1 Try block

Code in a try block becomes the monitoring area 12.2.2 Exception handler

Handling the place where the exception is thrown is called an "exception handler", which should have a handler for each caught exception, followed by a try block, and a catch representation of the keyword: 12.3 Create a custom exception

The way to customize an exception is usually to inherit an existing exception class from Java

Only a simple custom exception for the default constructor:

public class Simpleexception extends Exception {
} public

class Inheritingexception {public
    void F () throws S impleexception{
        System.out.println ("Throw simpleexception from F ()");
        throw new Simpleexception ();
    }

    public static void Main (string[] args) {
        inheritingexception sed = new inheritingexception ();
        try {
            sed.f ();
        } catch (Simpleexception e) {
            System.out.println ("Caught it!");

}} Output:
Throw simpleexception from F ()
caught it!

Custom exception with string parameter Builder:

/** * Custom exception with string parameter Builder * @date 2016/06/30 17:00/public class MyException extends Exception {public myexception ()
    {} public myexception (String message) {super (message); } public class FULLCONSTRUCTORSEXCP {public static void F () throws MyException {System.out.println ("Throw
        ing myexception from f ());
    throw new MyException ();
        public static void G () throws MyException {System.out.println ("throwing myexception from G ()");
    throw new MyException ("originated in G ()");
        public static void Main (string[] args) {try {f ();
        catch (MyException e) {e.printstacktrace (System.out);
        try {g ();
        catch (MyException e) {e.printstacktrace (System.out); Output: Throwing MyException from F () Com.mrdios.competencymatrix.java.readingnotes.ThinkingInJava.chapter12.MyExce Ption at Com.mrdios.competencymatrix. JAVA.READINGNOTES.THINKINGINJAVA.CHAPTER12.FULLCONSTRUCTORSEXCP.F (Fullconstructorsexcp.java:10) at Com.mrdios.competencymatrix.java.readingnotes.ThinkingInJava.chapter12.FullConstructorsExcp.main (
FULLCONSTRUCTORSEXCP.JAVA:20) throwing myexception from G () Com.mrdios.competencymatrix.java.readingnotes.ThinkingInJava.chapter12.MyException:Originated in G () COM.MRDIOS.COMPETENCYMATRIX.JAVA.READINGNOTES.THINKINGINJAVA.CHAPTER12.FULLCONSTRUCTORSEXCP.G ( FULLCONSTRUCTORSEXCP.JAVA:15) at Com.mrdios.competencymatrix.java.readingnotes.ThinkingInJava.chapter12.FullConstructorsExcp.main ( FULLCONSTRUCTORSEXCP.JAVA:25)
12.4 Abnormal and not inspected

The Java exception is divided into two categories: checkedexception (Client exception) unchecked exception (no exception)

Non-inspected exceptions are also called Run-time exceptions (RuntimeException), and the Java compiler does not require that it be caught or must throw an unhandled exception, but for a client exception, Java requirements must be captured in the method or continue to throw

For non-inspected exceptions, there are several ways to do this: capture continues to throw and does not handle

For the tested exception, the processing is as follows: continue to throw (negative practices) capture 12.4.1 Common exceptions with Try...catch

No exception was inspected: Exception filenotfoundexception ioexception SQLException

Inspected abnormality: NullPointerException classcastexception arrayindexsoutofboundsexception arithmeticexception (arithmetic anomaly)

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.