Dark Horse Programmer-----Java Basics: Exception and exception handling

Source: Internet
Author: User
Tags define exception finally block getmessage throwable try catch

-------Android Training, Java training, look forward to communicating with you! ----------

Exceptions overview

Exception: 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

The division of the problem: one is a serious problem, a non-serious problem

For serious, Java is described by the error class, and it is generally not written in a targeted code for error handling

For non-critical, Java is described by the exception class, and for exception can be handled with a targeted approach

Both error and exception have some common content

Example: abnormal information, cause, etc.

Throwable

|--error a serious problem

|--exception not a serious problem


Handling of exceptions

Java provides a unique statement to handle

Try

{The code that needs to be detected; }

Catch (Exception class variable)

{The code that handles the exception; (Processing mode)} Common methods Action String getMessage (): Get exception information

Finally

{A statement that is bound to execute; }


Declaring exceptions on a function for improved security, processing at the call, not processing compilation failures

handling of multiple exceptions

1, when declaring an exception, it is recommended to declare a more specific exception, so that the processing can be more specific

2, the other side declares a few exceptions, corresponding to a few catch blocks, do not define the extra catch block

If an inheritance relationship occurs in multiple catch blocks, the parent exception catch block is placed at the bottom

It is recommended that when catch processing is done, the catch must define the specific processing, not simply define E.PRINTSTACKTRACE (), or simply write an output statement

code example:

Class Demo

{

int div (int a,int b) throws exception//is functionally declared by the throws keyword that the feature may be problematic

{

return a/b;

}

}

Class Exceptiondemo

{

public static void Main (string[] args)

{

Demo d = new demo ();

Try

{

int x = D.Div (4,0);

System.out.println ("x =" +x);

}

catch (Exception e)//exception e = new ArithmeticException ();

{

SYSTEM.OUT.PRINTLN ("except 0");

System.out.println (E.getmessage ());//by Zero;

System.out.println (E.tostring ());//Exception Name: Exception information

E.printstacktrace ();//exception name, exception information, where the exception occurred

In fact, the JVM default exception handling mechanism is to call the Printstacktrace method,

Print trace information for the stack of exceptions

}

System.out.println ("over");

}

}


Custom Exceptions:

Because of the unique problems in the project, which are not described by Java and encapsulate the object, so for these unique problems can follow the concept of Java encapsulation of the problem, will be unique to the problem, to customize the exception encapsulation

when a throw throws an exception object inside a function, either in internal try catch processing or on the function declares the caller to handle

How to define exception information

In general, there is an exception in the function, the function needs to be declared, it is found that only the name of the exception in the printed result, but no exception information, because the custom exception does not define the information

Because the operation of the exception information has been completed in the parent class, the subclass simply passes the exception information to the parent class through the Super statement at the time of construction, so the custom exception information can be obtained directly through the GetMessage method.

Custom exception: must be a custom class inheritance exception

Inheritance exception Reason:

The anomaly system has a feature: because both the exception class and the exception object need to be thrown, they all have the parabolic, this parabolic is the unique feature of Throwable system, only classes and objects in this system can be throws and throw operation

The difference between throws and throw:

Throws is used on the function, followed by the exception class, you can follow multiple, separated by commas

Throw is used within a function, followed by an exception object

code example:

Class Fushuexception extends RuntimeException

{

Fushuexception (String msg)

{

Super (MSG);

}

}

Class Demo

{

int div (int a,int b) throws Exception

{

if (b<0)

throw new Exception ("There is a divisor is a negative case");

if (b = = 0)

throw new ArithmeticException ("was 0 apart");

return a/b;

}

}

Class ExceptionDemo3

{

public static void Main (string[] args) throws Exception

{

Demo d = new demo ();

int x = D.Div (4,-1);

System.out.println ("x =" +x);

System.out.println ("over");

}

}


RuntimeException:

There is a special subclass exception in exception: RuntimeException runtime exception

If the exception is thrown in the function's contents, the function can be compiled by

Cause: Because there is no need for the caller to process, when the exception occurs, you want the program to stop, because at run time, there is no way to continue the operation of the situation, you want to stop the program, the code is corrected

If the exception is declared on a function, the caller can do it without processing and compile the same


Custom exception: If the exception occurs and no further operations can be performed, let the custom exception inherit RuntimeException

There are two types of exceptions:

1, exception detected at compile time

2, exception not detected at compile time (runtime exception, i.e. RuntimeException and its subclasses)

V. Finally block of code:

code that must be executed, usually to close the resource

Special statement: system.exit (0);//System exit, JVM end

Remember that catch is used to handle exceptions, and if no catch means that the exception has not been processed, and if the exception is a detection-time exception, you must declare

Vi. the embodiment of the exception in the child parent category coverage:

When a subclass overrides a parent class, if the method of the parent class throws an exception, the child class's overriding method can only throw exceptions to the parent class or subclasses of the exception

If the parent method throws more than one exception, the child class can only throw a subset of the parent exception when overriding the method

If there is no exception thrown in the method of the parent class or interface, then the subclass cannot throw an exception when overriding the method, and if the subclass method has an exception, it must be done with a try, absolutely not thrown


This article is from the "Dot Drop" blog, please be sure to keep this source http://arctictern.blog.51cto.com/10120640/1659828

Dark Horse Programmer-----Java Basics: Exception and exception handling

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.