Java Learning anomalies

Source: Internet
Author: User

Abnormal
What type of error can cause an exception?
Serious hardware errors, such as some chip failures in memory or hard drive crashes.
Programs need to use I/O devices that are not currently available in the system
Trying to remove it with 0
An attempt was made to access an element of an array through a subscript beyond the bounds
Integer overflow
Floating-Point Overflow
An attempt was made to open a file that does not exist to use
Attempts to break security, such as attempting to modify a read-only file

try {
Blocks that accept monitoring, which can produce exceptions in this area
The code to test

} catch (Someexceptiontype e) {
Handling of someexception when it occurs
} catch (Error type exception e) {
Handling of exception when it occurs
}

try{} blocks typically contain a piece of code in a Java program that may cause unexpected events to run. Try means that you need to detect the execution of this code.
Catch program Block:
The syntax is: catch (Exceptionclass exceptionobj) {}
The catch program block must appear after the try program block. A try program block can be followed by one or more catch blocks.
The catch block is skipped directly when no exception occurs in the TRY program block.
When an exception occurs in a try block, the catch block that matches the current exception is executed.

public class exceptiontest{
public static void Main (String args[]) {
int b=0; int A;
try {a=4/b;}
catch (ArithmeticException e) {
System.out.println ("divided by 0");}
}
}


What exceptions are thrown to deal with

Error and RuntimeException not to be won
Only ioexception need to pounce.

Error: Describes the errors inside the Java Runtime system and the resource exhaustion situation. Users generally do nothing about this kind of error. (such as errors inside the virtual machine, memory overflow, stack overflow errors)
Classification of exceptions:
Unchecked exception refers to the error class and the RuntimeException class and their subclasses.
The exceptions represented by RuntimeException and its subclasses are often caused by program-written errors.
For example, array out-of-bounds errors, null pointer errors, and so on, this type of error can often be avoided by rewriting the program.
Unchecked Exception can not be captured in the program.

Common exception classes in Java programs
RuntimeException:
Wrong Transformation (CAST): Java.lang.ClassCastException
Array out-of-bounds error: java.lang.ArrayIndexOutOfBoundsException
Null pointer error: java.lang.NullPointerException
Array element type error: Java.lang.ArrayStoreException
Arithmetic operation error: Java.lang.ArithmeticException
Error when string is converted to numeric type: java.lang.NumberFormatException
Non-runtimeexception:
File read out of bounds: java.io.EOFException
File not found: java.io.FileNotFoundException
URL link Error: java.net.MalformedURLException
Host not recognized: Java.net.UnknownHostException

Throw
If a throw statement is encountered in the program execution, the normal process is interrupted, and the exception is handled in the catch block.
Throws, the program as far as possible to toss.

Finally-will definitely execute the block (note not final)
Exception has a unified egress--finally{} block

try {
The regular code;
}
catch () {

Handling Exceptions

}
finally {
Whatever happens (or does not happen), the part to be executed;
}

note:finally{} code Snippets are useful in some cases, such as when an exception occurs, we also need to close the connection to the database, close the file, close the open port, close the network connection, and so on. These are all needed to be considered in a complete and rigorous procedure.


File File = new file ("F:\\day07\\bam_teacher\\mycode\\src\\org\\kelan\\day09\\text.txt");
Note that the method of referring to the path in Java, to use the double "\ \" Separate, because the single "\" may be and the following, such as \ t is a character, double "\ \" the previous "\" is a transfer character, tell the back of a "\" is a file path "\".

The difference between the file class and the Stream class:
The file class is used primarily to describe how files on a system are stored on disk.
The steam class mainly operates on the contents of the file, for example, we must access the file via FileInputStream, FileOutputStream, or randomaccessfile streams.

InputStreamReader for converting byte stream into character stream

Copy a file by byte

Java Learning anomalies

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.