Different Java Learning (vi) inheritance under (2.5) exception on

Source: Internet
Author: User
Tags define exception getmessage try catch

1, Exceptions-Overview

Exception: This is when the program is running in an unhealthy situation.

The origin of the anomaly: The problem is also a concrete thing in real life,

It can also be described in the form of a Java class and encapsulated as an object.


In fact, Java describes the abnormal situation after the object embodiment, through the JVM exception handling mechanism.

The problem is divided into two kinds: one is a serious problem, a non-serious one.


for Serious , Java is described by the error class.

For error, it is generally not written in a targeted code to handle it.

Cases:

Class Exceptiondemo{public static void Main (string[] args) {byte[] arr=new byte[1024*1024*190];}}


for non-critical , Java is described by the exception class.

For exception, you can use a targeted processing method for processing.

Cases:



Whether error or exception have some common content,

For example: abnormal information, causes of the cause and so on.

Throwable (can be thrown, is the superclass of all exceptions)

|--error

|--exception

Subclass commonality in the error class: End with Error

Subclass commonality in the Exception class: End With exception ending, disease


2. Exception-handling

Java provides unique statements for processing, for exception

Try

{

Code that needs to be detected;

}

catch (Exception class variable)

{

Code to handle exception: (processing mode)

}

Finally

{

Statements that are bound to be executed;

}



common method operations are performed on the caught exception object.
E.getmessage ();

E.tostring (); Exception Name: Exception information

E.printstacktrace ();

catch (Exception e) {System.out.println (e); System.out.println (E.getmessage ()); System.out.println (E.tostring ()); E.printstacktrace ();//Exception Name: Exception information, where the exception occurred. In fact, the JVM default exception handling mechanism is to call Printstacktrace (), print the exception stack trace information. }



3. Exception-declaration

Throws exception//the functionality of the throws keyword declares that the feature may be problematic.


Class Demo{int div (int a,int b) throws Exception<span style= "Font-family:microsoft Yahei;" >//statement may be problematic </span>{return A/b;}} Class Exceptiondemo1{public static void Main (string[] args) {demo d=new demo (); int x=d.div (4,1); SYSTEM.OUT.PRINTLN (x); System.out.println ("Over");}}

This is to tell you that the Div method in this demo class may be problematic, with Try-catch ()


4. Multiple exception handling

For multiple exception handling,

1, when declaring an exception, declare a more specific exception, so that the processing can be more specific.

The function has an exception and ends immediately.

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

If there are inheritance relationships in multiple catch blocks, the parent exception block is at the bottom.


When you set up catch processing, you must define a specific processing method in the catch.

Don't simply define a e.printstacktrace (), or simply write an output statement.


5. Custom Exceptions

You have found that the Java system is divided into two major factions, anomalies and errors.

The exception has many younger brothers, these younger brother's appearance, is because Java has the common question to encapsulate.

For example, arithmetic anomalies, and the exception of the foot Mark encapsulation. Then there is a small problem,


Because there are unique problems in the project, these problems are not described and encapsulated by Java.

So for these unique problems you can follow the idea that Java encapsulates the problem.

The unique problem is to be encapsulated with custom exceptions.


Requirements: In this program, for the divisor is-1, also considered to be wrong is unable to perform the operation.

Then you need to have a custom description of the problem. Manually thrown.


When a throw throws an exception object inside the function, the corresponding processing action must be given.

Either in-house try catch processing.

It is either declared on the function to be handled by the caller.

In general, an exception occurs within a function, and a declaration is required on the function.

Results:

Found only the name of the exception in the printed result, but no information about the exception.

Because the custom exception does not define the information


How do you define exception information? Modify the above file

Class Fushuexception extends Exception{private String msg; Fushuexception (String msg) {this.msg=msg;} Public String GetMessage () {return msg;}} Class Demo{int div (int a,int b) throws Fushuexception{if (b<0) throw new Fushuexception ("There is a case where divisor is negative/by Fushu");// Manually throws a custom exception object through the Throw keyword. return a/b;}}
Results:

ToString () call GetMessage ()

Simplified:

Class Fushuexception extends Exception{fushuexception (String msg) {super (MSG);}}

because the operation of the exception information has been completed in the parent class,

So as long as the subclass is constructed, the exception information is passed to the parent class through the Super statement.

The custom exception information can then be obtained directly from the GetMessage method.


Throw error value:

Class Fushuexception extends exception{private int value; Fushuexception (String msg,int value) {super (msg); this.value=value;} public int GetValue ()           //Multiple defines a GetValue method {return value;}} Class demo{int div (int a,int b) throws fushuexception {if (b<0)        throw new Fushuexception ("There is a case where the divisor is negative/by Fushu", b) ;//Manually throw a custom exception object through the Throw keyword. return a/b;  }} Class exceptiondemo3{public static void Main (string[] args) {demo d=new demo (); try{int X=d.div (4,-3); System.out.println ("x=" +x); catch (Fushuexception e) {System.out.println (e.tostring ());//system.out.println ("negative"); SYSTEM.OUT.PRINTLN ("The negative number of the error is:" +e.getvalue ());//Gets the value returned by the Gervalue Method}}}

Summary of custom Exceptions:

Must be a custom class to inherit exception or error or Throwable.

Inheritance exception Reason:

The exception system has a feature that both the exception class and the exception object are thrown.

They all have a parabolic nature, which is unique to the throwable system.


Only classes and objects in this system can be throws and throw.


The difference between throws and throw:

Throws is used on the function,

Throw is used within a function;

Throws followed by an exception class. You can follow multiple, separated by commas.

Throw is followed by an exception object.


Different Java Learning (vi) inheritance under (2.5) exception on

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.