第十六次课
一.package com.demo1;
/**
- 出现error
- Exception in thread “main” java.lang.StackOverflowError
- @author Administrator
*/
public class Demo1 {
private int i;
private Demo1 demo1;
public Demo1() {
demo1 = new Demo1();
}
public void test() {
}
public static void main(String[] args) {
Demo1 d = new Demo1();
System.out.println("iiii");
}
}
二.
package com.demo1;
import java.io.File;
import java.io.IOException;
/**
*
- 异常的执行情况,及异常跟踪演示。
- @author Administrator
*/
public class Demo2 {
public static void main(String[] args) {
TestClass t = new TestClass();
t.test();
}
}
class TestClass{
public void test() {
int[] num = new int[5]; // num[0] 1,2,3,4
System.out.println(num[num.length - 1]); // 运行时异常( );不需要处理。
int a = 1;
int b = 2;
System.out.println(a + b);
// 检测异常/ 非运行时异常 / 编译时异常 / 需要我们处理。
File f = new File("c://hh.txt");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}// try之后,并没有终止程序。接着执行。
int x = 1;
int y = 2;
System.out.println(x + y);
}
}
另一个文件
一.
package com.demo2;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
/*
- 如何处理Exception异常
- 处理异常的方式:抓抛模型
- 1.抛:当执行程序时,一旦出现异常,就会生成一个对应异常类型的对象,并将对象抛出
- ①.抛的类型:自动抛和手动抛
- ②.此异常的对象将来是交给方法的调用者
- 2.抓:抓住程序中抛出来的异常对象。
- 异常处理的方式:
- 1.try-catch-finally
- try{
-
//可能会发生异常的代码
- }catch(Exception e1){
-
// 处理方式一
- }catch(Exception e2){
-
//处理方式二
- }finally{
-
// 一定要执行的代码
- }
- ①.try代码块里面的声明的变量,是局部变量,作用域是在代码块中
- ②.finally是可选的,如果加了finally,不论是否出现异常,都会执行finally中代码
- ③.catch语句是用来处理异常
-
getMessage();printStrackTrace():打印异常信息(程序出错的位置信息及原因)
- ④.catch代码块可以有多个,try中抛出的异常对象是从上往下去匹配catch中的异常类型,一旦满足就行
-
catch中的代码。执行完毕,就跳出所有的catch。
- ⑤.如果catch中多个异常类型是并列关系,不需要考虑顺序问题。
-
如果catch中多个异常类型有继承关系,子类需要在父类异常对象的上面。
- ⑥.try-catch是可以嵌套多层。
- 提示:
- 1、对于运行时异常,可以不用显示的进行异常处理。
- 2、对于编译时异常,必须要显示的进行异常处理。
*/
public class Demo1 {
public static void main(String[] args) {
int[] num = new int[5];
try {
int x = 0;
//System.out.println(num[5]);
}catch(InputMismatchException e) {
e.printStackTrace();
System.out.println("捕获到异常。。。");
} // ArrayIndexOutOfBoundsException
// 检测异常的处理
File file = null;
try {
file = new File("xxxx");
file.createNewFile();
Class.forName("");
Thread.sleep(124);
}catch(IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}
try {
file = new File("xxxx");
file.createNewFile();
Class.forName("");
Thread.sleep(124);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(file);
int a = 1;
System.out.println(a);
test();
//test1();
test2();
}
// 类型转换异常 ClassCastExcetion
public static void test1() {
Object object = LocalDateTime.now();
String str = "";
str = (String)object;
}
// 空指针异常 NUllpointerExcetion
public static void test2() {
List s = getList();
if(s != null) {
System.out.println(s.size());
}
}
public static List getList() {
//
List l = new ArrayList<String>();
l.add("a");
return null;
}
// / 2,算术异常 12/ 0
public static void test() {
int i = 12;
try {
System.out.println(i / 0);
}catch (ArithmeticException e) {
}
System.out.println("sssssssssss");
}
// 除了RuntimeExpetion及子类之外的异常。都是编译异常,都需要通过try()或抛出的方式处理。
public void test4() throws FileNotFoundException{
FileInputStream fs = new FileInputStream("XXX");
}
}
二.
package com.demo2;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
- finally语句块使用
- 注意: 有return的使用
- @author Administrator
*/
public class Demo2 {
public static void main(String[] args) {
InputStream s = null;
try {
s = Demo2.class.getClassLoader().getResourceAsStream("jdbc");
Properties p = new Properties();
p.load(s);
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
} finally {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("end");
test();
}
public static int test() {
try {
int s = 10 / 0;
System.out.println("3=====");
return 10;
}catch (Exception e) {
}finally {
System.out.println("1=====");
}
System.out.println("2=====");
return 0;
}
}
另一个文件
一.
package com.demo3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
- 自定义异常 ,及非运行时异常转换
- @author Administrator
*/
public class Demo1 {
public static void main(String[] args) throws FileNotFoundException {
//test();
//test1();
// 异常可处理,可不处理。
test2();
}
// 运行时异常。
public static void test2() throws ArithmeticException{
try {
int x = 12 / 0;
}catch (ArithmeticException e) {
// 自己处理
e.printStackTrace();
// throw
throw new ArithmeticException();
}
System.out.println("end");
// 可处理也可不处理
test3();
}
// 把检测异常转换为运行时异常。
public static void test3() throws MyRunTimeException {
try {
FileInputStream input = new FileInputStream(new File("xx"));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new MyRunTimeException("文件不存在,需要观察一下文件目录。");
}
try(FileInputStream input = new FileInputStream(new File("xx"))){
}catch (Exception e) {
// TODO: handle exception
}
}
public static void test() throws FileNotFoundException{
FileInputStream input = new FileInputStream(new File("xx"));
}
public static void test1() {
try {
FileInputStream input = new FileInputStream(new File("xx"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class MyRunTimeException extends RuntimeException{
public MyRunTimeException(String s) {
super(s);
}
}
二.
package com.demo3;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
- 异常处理的方式一:try-catch-finally(自行处理)
- 异常处理的方式二:在方法的声明处,显示的抛出异常的对应类型(委托处理)
- 格式:public void method() throws IoException,FileNotFoundException
- ①.当此方法内部出现异常时,会抛出一个异常类的对象,抛给方法的调用者
- ②.异常对象可以一直向上抛,直到Main函数,也可以通过try-catch进行处理异常。
*/
public class Demo2 {
public static void main(String[] args) {
try {
m2();
} catch (MyException e) {
e.printStackTrace();
}
}
public static void m4() throws MyException{
int x = 1;
int b = 2;
if(x > b){
throw new MyException("出现错误");
}
}
public static void m2() throws MyException{
try {
m1();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new MyException("反生了io异常");
}
}
public static void m1() throws IOException,FileNotFoundException {
FileInputStream f = new FileInputStream("xx");
f.close();
}
}
class MyException extends Exception{
public MyException() {}
public MyException(String mes) {
super(mes);
}
}``