The anomaly of Javase __java the road

Source: Internet
Author: User
Tags exception handling try catch
exception handling .... :

public static void Main (string[] args) {
        System.out.println ("Start of Program");
        Try is just an attempt to write a code that might appear to be abnormal
        try{
        String str = null;
         * * When a null pointer appears, the JVM creates an instance of the null pointer exception and throws it
         out
        /System.out.println (Str.length ());
        Catch catches the exception, and then how to resolve
        }catch (NullPointerException e) {
            System.out.println ("null pointer appears");
        }

        System.out.println ("End of Program");
        }

you can write multiple catch
More can write Exception e ~ ~ ~ ~
It's best to capture Exception eventually.
finally the role

code that is specified regardless of whether the exception is finally executed
usually we do cleanup in finally.
like releasing connections, shutting down streams, etc.

public static void Main (string[] args) {

        try{
            System.out.println ("Establish a connection to the database");
            System.out.println ("connected successfully");
            SYSTEM.OUT.PRINTLN ("Querying Data from Database");
            SYSTEM.OUT.PRINTLN ("Operation of Data");
            String str = "a";

            int i = Integer.parseint (str);
            SYSTEM.OUT.PRINTLN ("Data processing completed.") ");
            SYSTEM.OUT.PRINTLN (save data to database);

        catch (Exception e) {
            System.out.println ("error occurred");
            Close some connections with finally. Action
        }finally{
            System.out.println ("Disconnect from database") regardless of whether an exception occurred;
            }
    
Throw keyword

The responsibility for handling the current exception is not the way it is, just throw it to him.
Meet the requirements of the grammar, but do not conform to the business logic, active throw an anomaly out

For example, a person's age input exceeds the rules,
And then just
throw new Runtimeeexception ("Age is not normal");
Actively throws an exception

public class Person {

    private int age;



    Public person () {
        //TODO auto-generated constructor stub
    } public



    int getage () {return age
        ;
    }



    public void Setage (int age) {
        if (age<=0| | AGE>100) {
//runtime exception
            throw new RuntimeException ("Not human Age");
        }
        This.age = age;
    }

    public static void Main (string[] args) {person
        p = new Person ();
        P.setage (a);
    }

Or declare the throw of the class exception on the current method, so that the caller can be notified to handle the exception

That is either to catch the exception itself or to throw it. However, exceptions must be handled.

do not declare throw on the Mian method .... Common RuntimeException

throw When overriding a method

This means that subclasses do not have to write exceptions when they override a method.
Either throws a partial exception, or is a subclass exception that throws an exception in the parent class method exception Common APIs

Printstacktrace
Used to track the contents of the execution stack when an exception event occurs.
E.printstacktrace

!!!!!
Normally, if an exception instance is throw in the method, we must handle the exception. There are two ways to handle this:
1. Add a try catch for your throw
2. The second method declares the throw of the class exception. To make it easier to tell the caller to handle the exception. get the reason for the error

Getcause

Custom Exception

That's the age that was written earlier that didn't fit the human range.
Your own definition produces an exception
Throw new, what's the anomaly?

Throw and throws differences:
Throw is an action thrown
Throws notifies the caller to handle the exception. Java Proximity principle

Pass in the argument null.
is not near the call, the closer the subclass
The nearest call, the child class is called precedence, the more subclasses call. Like what
ArrayList and List then ArrayList priority subclasses
No subclass next on interface and abstract class. If there is a parallel relationship will be an error.

Basic type and object on object

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.