Java learning diary 9-exception, Java learning diary 9-

Source: Internet
Author: User

Java learning diary 9-exception, Java learning diary 9-

Exception)

1. What is an exception?

Exceptions are errors in the program, such as array out-of-bounds and NULL pointer access. In Java, everything is an object, and exceptions are no exception. All exceptions are an instance object derived from the Throwable class.

Ii. Exception Classification

In the exception field, there are two major genres. One class is called the Error class, and the other class is called the Exception class. They all inherit from the Throwable class. There are two major branches in the Exception class: The IOException class and the RuntimeException class.

RuntimeException is an exception caused by program errors.

IOException is an exception caused by IO errors.

Example:

RuntimeException

IOException

Array access out of bounds

Trying to open a nonexistent file

Access a null pointer

Try to read data at the end of the file

Incorrect type conversion

 

The RuntimeException class is similar to its uncle Error class. The uncles are collectively referred to as unchecked exceptions. IOException and its brother and uncle are obviously not all the way. It is called checked Exception, which means you have to manage it.

 

3. Exception handing)

We didn't need to worry about the unchecked exceptions, so here we only talk about how to deal with the checked exceptions.

(1) throws

First, you have to declare it. The method should use throws + Exception name in its header to declare all possible exceptions.

4 situations where an exception is thrown:

1. Call a method to throw a checked exception.

2. An error is found during the running of the program and throws the checked exception using the throws statement.

3. An error occurs in the program. For example, if array access is out of bounds, ArrayIndexOutOfBoundsException is automatically thrown.

4. Internal errors of Java Virtual Machine and Runtime Library

For example:

1 Class MyAnimation{2     public Image LoadImage(String s)throws IOException, EOFException3     {4     ......5     }6 }

(2) throw an exception with the throw keyword

 1 String readData(Scanner in)throws EOFException{ 2     ... 3     while(...){ 4         if(!in.hasNext()) 5         { 6             if(n<len) 7             throw new EOFException(); 8         } 9     }10 }        

(3) custom exceptions

When exceptions provided by java cannot meet your needs, you can DIY one by yourself.

1 // define a FileFormatException class 2 class FileFormatException extends IOException {3 public FileFormatException () {} 4 public FileFormatException (String gripe) {5 super (gripe ); 6} 7} 8 // The following throws a custom exception 9 String readData (bytes in) throws EOFException10 {11... 12 while (...) {13 if (ch =-1) {14 if (n <len) 15 throw new FileFormatException (); 16} 17} 18 return s; 19}

(4) Capture exceptions

If you throw an exception, You have to capture it and handle it.

1 // catch exceptions using the try-catch Block. 2 try {3 code4} 5 catch (ExceptionType e) {6 handler for this type7}

If the code in the try statement block throws an exception class described in the catch clause

1) The program will skip other code of the try statement block.

2) The program will execute the processor code in the catch clause.

(5) exception-internal and external Methods

For exceptions, you can use the try-catch Block in the method to capture and handle them. If you do not want to process exceptions in the method, you can declare the exceptions that may be thrown in the method header, handle exceptions that may be thrown by the code that calls the method. In short, try-catch is used in the method, and throws are used for external use.

(6) capture multiple exceptions

1 try{2     code3 }4 catch(FileNotFoundException | UnknowHostException e){5     ...6 }7 catch(IOException e){8     ...9 }

(7) finally

1 try{2     ...3 }4 catch(IOException e){5     ...6 }7 finally{8     ...9 }

No matter whether an exception occurs or not, the code in the finally statement block is executed.

Try-catch, try-catch-finally, and try-finally combinations. Combinations 1 and 3 are more commonly used.

(8) try statement block with resources

When a try block with resources exits, the resource is automatically closed, which can save the finally statement.

1 try (bytes in = new bytes (new FileInputStream ("file path") {2 while (in. hasNext () 3 System. out. println (in. next (); 4}

Note: Java core technology volume I

 

 

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.