package com.njwb18081.day16
import java.io.File
import java.io.FileInputStream
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Scanner
public class Test01 {
public static void main(String[] args) {
// 编译时异常
// DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
// df.parse("1977/07/07 07:07:07")
//将硬盘的文件读取的java程序中
// FileInputStream fis=new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\jdk-8u161-windows-x64.exe"))
// 运行时异常
// String str=null
// System.out.println(str.equals("abc"))
// System.out.println(1/0)
// int[] arr=new int[4]
// System.out.println(arr[-1])
// Scanner sc=new Scanner(System.in)
// System.out.println("请输入数字:")
// System.out.println(sc.nextInt())
}
}
package com.njwb18081.day16
import java.io.FileInputStream
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.InputMismatchException
import java.util.Scanner
public class Test02 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in)
try{
System.out.println("请输入一个整数的除数:")
int i=1/sc.nextInt()
System.out.println(i)
// DateFormat df=new SimpleDateFormat("yyyy/MM/dd")
// df.parse("1999-01-01")
}catch(ArithmeticException e){
// 针对于出现ArithmeticException的处理
System.out.println("0不能作为除数")
}catch (InputMismatchException e) {
System.out.println("你输入内容不符合规范")
}catch(Exception e){
System.out.println(e)
System.out.println(e.getMessage())
e.printStackTrace()
}finally{
// 无论执行时try还是catch都会执行到finally
System.out.println("执行结束...")
}
System.out.println(4444444)
}
}
package com.njwb18081.day16;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Test03 {
public static void main(String[] args) {
Test03 t=new Test03();
try{
t.fn();
}catch (ParseException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
System.out.println("程序执行出错,请稍候....");
}
System.out.println("---------------------------");
try{
t.fn2();
}catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("---------------------------");
try {
t.fn3();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void fn() throws ParseException{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
public void fn2() throws Exception{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
df.parse("1977/07/07 07:07:07");
}catch (ParseException e) {
throw new Exception("格式转换有问题");
}
}
public void fn3() throws Exception{
try{
int i=1/0;
}catch (ArithmeticException e) {
throw new Exception("这个方法中出现问题了");
}
}
}
package com.njwb18081.day16;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Test03 {
public static void main(String[] args) {
Test03 t=new Test03();
try{
t.fn();
}catch (ParseException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
System.out.println("程序执行出错,请稍候....");
}
System.out.println("---------------------------");
try{
t.fn2();
}catch(Exception e){
System.out.println(e.getMessage());
}
System.out.println("---------------------------");
try {
t.fn3();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void fn() throws ParseException{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
}
public void fn2() throws Exception{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try{
df.parse("1977/07/07 07:07:07");
}catch (ParseException e) {
throw new Exception("格式转换有问题");
}
}
public void fn3() throws Exception{
try{
int i=1/0;
}catch (ArithmeticException e) {
throw new Exception("这个方法中出现问题了");
}
}
}
package com.njwb18081.day16;
public class MyRuntimeExceprion extends RuntimeException {
public MyRuntimeExceprion() {
super();
}
public MyRuntimeExceprion(String message, Throwable cause) {
super(message, cause);
}
public MyRuntimeExceprion(String message) {
super(message);
}
public MyRuntimeExceprion(Throwable cause) {
super(cause);
}
}
package com.njwb18081.day16;
public class Test04 {
public static void main(String[] args) {
Test04 t=new Test04();
try {
t.fn();
} catch (MyException e) {
System.out.println(e.getMessage());
}
System.out.println("---------------------------");
try{
t.fn2();
}catch (MyRuntimeExceprion e) {
System.out.println(e.getMessage());
}catch (Exception e) {
System.out.println(e);
}
}
public void fn() throws MyException{
int[] arr=new int[4];
try{
System.out.println(arr[-1]);
}catch(Exception e){
throw new MyException("程序出错");
}
}
public void fn2(){
try {
int i=1/0;
} catch (Exception e) {
throw new MyRuntimeExceprion("计算有误");
}
}
}
总结
- 一张图
- 五个关键字(try,catch,finally,throws,throw)
- 先逮小的,后逮大的
- 异常和重写的关系
- 自定义异常