0x00 Preface
What is anomaly?
The event that causes the normal flow of a program to be interrupted is called an exception.
content
1.try catch
0x01 try catch
How to use: try catch contains content
Note: the program can continue to run without interruption
Raise a chestnut.
package first;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Demo {
public static void main(String[] args) {
File f=new File("d:/123456798.exe");
try{
new FileInputStream(f);
System.out.println("Successfully opened");
}catch(FileNotFoundException e)
{
System.out.println("file does not exist");
}
}
}
A reference to a subclass.
Another chestnut
package first;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Demo {
public static void main(String[] args) {
File f=new File("d:/123456798.exe");
try{
new FileInputStream(f);
System.out.println("Successfully opened");
}catch(Exception e)
{
System.out.println("file does not exist");
}
}
}
Reference to the parent class.
0x02 catch multiple exceptions
1. Capture separately
2. Capture together
package first;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
File f=new File("d:/123456798.exe");
try{
new FileInputStream(f);
System.out.println("Successfully opened");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2018-03-28");
}catch(FileNotFoundException | ParseException e)
{
if (e instanceof FileNotFoundException)
System.out.println("file does not exist");
if (e instanceof ParseException)
System.out.println("Date format parsing error");
}
}
}
0x03 final
Code in finally is executed, no matter what happens.
Raise a chestnut.
package first;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] args) {
File f=new File("d:/123456798.exe");
try{
new FileInputStream(f);
System.out.println("Successfully opened");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2018-03-28");
}catch(FileNotFoundException | ParseException e)
{
if (e instanceof FileNotFoundException)
System.out.println("file does not exist");
if (e instanceof ParseException)
System.out.println("Date format parsing error");
}finally{
System.out.println("No matter what happens, I will do it!");
}
}
}
Effect
0x04 exception classification
1. Exceptions
2. Abnormal operation
3. mistake
1. Exceptions
An exception that must be handled. try catch, or throw out.
2. Abnormal operation
An exception that does not require a try catch.
Common operation exception: divisor is not 0
Subscript boundary crossing
Null pointer exception
3. mistake
System level exception, usually out of memory.