Java Exception Handling

Source: Internet
Author: User
Tags try catch

A Java exception classification

Java Exceptions:
(1) Error:java serious problems that virtual machines cannot solve
(2) Exception: null pointer exception access not present file network connection interrupted
Both error and exception inherit the child java.lang.throwable
Error type:
1.java.lang.stackoverflowerror
public class Testerror {
public static void Main (string[] args) {
Main (args);
}
}
2.java.lang.outofmemoryerror
public static void Main (string[] args) {
Byte[] B=new byte[10241024600];
}
Exception:
Common run-time exceptions
1. Array Subscript exception Java.lang.arrayindexoutofboundsexception:10
@Test
public void Test () {
int []i=new int [10];
System.out.println (i[10]);

}
2. Arithmetic exception: ArithmeticException
@Test
public void Test2 () {
int i=10;
System.out.println (i/0);
}
3. Type Conversion Exception: Java.lang.ClassCastException:java.util.Date cannot is cast to
Java.lang.String
@Test
public void Test3 () {
Object obj=new Date ();
String str= (string) obj;
}
4. Null pointer exception java.lang.NullPointerException
@Test
public void Test4 () {
Object s=new String ("abc");
S=null;
System.out.println (S.tostring ());

}
5. Run at compile time
@Test
public void Test5 () throws exception{
FileInputStream fis=new FileInputStream (New File
("Hello.txt"));
int b;
while ((B=fis.read ())!=-1)
{
System.out.println ((char) b);
}
}
Two. How to handle exception exceptions
Java provides a catch-and-throw model of exceptions
The first step:
"Throw": When we execute the code, an exception is generated and a corresponding exception class object is created at the exception code.
and throws this object (automatically thrown, manually thrown)
> Once the object of the exception class is thrown, the program terminates execution
> The object of this exception class is thrown to the caller of the method
2. "Catch": Catch the object of the exception class thrown out of the previous step, how to catch the way of exception handling
Provides two ways to handle an object of an exception class
> Treatment Method One:
try{
Code that may appear to be abnormal
}
catch (Exception ex) {
}
catch (Exception ex2) {
}
finally{
}
Note:
Variables declared within 1.try, similar to local variables, cannot be called except for a try statement
The 2.finally statement is optional
Inside the 3.catch statement is the handling of the exception object
There are two methods available: GetMessage () printstacktrace ();
4. There can be multiple catch statements, and the exception class object thrown in the try matches the type of exception in the catch, once the full
Execute the code in the catch statement, and after execution, jump out of the multiple catch statements
5. If the exception is handled, then the subsequent code continues to execute
6. If multiple exception types in a catch are "side-by-side" relationships, the order can be;
If it contains a relationship, then the subclass is above
The 7.finally statement is bound to be executed, regardless of whether the statement in the catch has an exception or a return statement
8.try catch can be nested with each other
> Treatment Method Two:
The list of thrown exceptions is declared in the method declaration with the throws statement, and the exception type declared by throws can be the same as that produced in the method.
A constant type, or it can be its parent class
Format: public static void Method2 () throws IOException
When an exception occurs inside a method, an exception object is thrown to the caller of the method
The object of the exception can be thrown up and down, up to main, and, of course, in the process of throwing up, you can go through the try-catch.
Acting

Three. For run-time exceptions, you can handle them without explicit processing
Must be handled for compile-time exceptions
Summary: Java exception handling using scratch-and-throw model
1. Catch: Exception handling, there are two ways (try-catch-finally throws+ exception type)
2. Throw: Once executed, an exception occurs, throwing an object of an exception class (auto-throwing: throw+ exception
Item
The > Exception class can be either a ready-made exception class or a self-created

Four.
How to customize exception classes:
1. Let the custom exception class inherit the existing exception class
public class MyException extends RuntimeException {
2. Provide a serial number that provides several overloaded constructors
Static final Long serialversionuid = -7034897190745766939l;
Public MyException () {

}
Public myexception (String msg) {
Super (MSG);
}
}
Subclasses override the parent class's method, which throws an exception type that can only be a subclass of the exception class of the overridden method or the exception class. That is, sub-class
The exception range to throw is less than the parent class.

Java 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.