本篇文章是根据视频学的,来源如下所示:
| 教程来源: | java基础到高级_零基础自学Java–尚硅谷–宋红康 |
| 教程视频地址: | java基础到高级_零基础自学Java--尚硅谷--宋红康_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1ny4y1Y7CW |
目录
1.局部内部类的使用的第一个注意点
2.异常概述
3.异常的分类
4.常见异常的举例
5.异常处理方式概述
6.处理异常:try-catch方式
7.finally的使用
8.编译时的异常与运行时的异常的不同处理方式
9.异常处理:throws方式
10.重写方法异常抛出的规则
11.开发中如何选择哪种方式处理异常
12.手动抛出异常对象
13.如何自定义异常
14.异常处理练习基本使用
15.异常处理练习综合使用
16.异常处理章节总结
1.局部内部类的使用的第一个注意点
package com.atshangguigu.java;
public class InnerClassTest {
public void onCreate()
{
button.setonClickListener(new View.onClickListener(){
public void onClick()
{
System.out.println("hello");
}
});
}
//局部变量
int num = 10;
class AA
{
public void show()
{
System.out.println(num);
}
}
}
2.异常概述
- 异常:在Java语言之中,将程序执行中发生的不正常情况称之为"异常"(开发过程之中的语法错误和逻辑错误不是异常)
- Java程序在执行的过程之中所发生的异常事件可以分为两类:
Error
:
Java
虚拟机无法解决的严重问题。如:
JVM
系统内部错误、资源耗尽等严重情况。比如:
StackOverflowError
和
OOM
。一般不编写针对性的代码进行处理。(严重的问题)
Exception:
其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如:
空指针访问
试图读取不存在的文件
网络连接中断
数组角标越界
package com.atshangguigu.java;
public class ErrorTest {
public static void main(String[] args) {
//A.Error
//1.栈溢出:java.lang.StackOverflowError
//main(args);
//2.堆溢出:java.lang.OutOfMemoryError: Java heap space
//Integer[] arr = new Integer[1024 * 1024 * 1024];
//B.Excpeption(狭义上的异常)
}
}
3.异常的分类
-
对于这些错误,一般有两种解决方法:一是遇到错误就终止程序的运行。另一种方法是由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。
捕获错误最理想的是在编译期间,但有的错误只有在运行时才会发生。比如:除数为0,数组下标越界等
分类:编译时异常和运行时异常
package com.atshangguigu.java;
public class ExceptionTest {
}
4.常见异常的举例
package com.atshangguigu.java;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionTest {
@Test
public void test07()
{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
//6.ArithmeticException(算数异常)
@Test
public void test06()
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
//5.InputMismatchException
@Test
public void test5()
{
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
scanner.close();
}
//4.NmberFormatException
@Test
public void test4()
{
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//3.ClassCastException
@Test
public void test3()
{
Object obj = new Date();
String Str = (String)obj;
}
//2.IndexOutOfBoundsException
@Test
public void test2()
{
//(1)ArrayIndexOutOfBoundsException
// int[] arr = new int[10];
// System.out.println(arr[10]);
//(2)StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}
//1.NullPointerException
@Test
public void test1()
{
//(1)
// int[] arr = null;
// System.out.println(arr[15]);
//(2)
String str = "abc";
str = null;
System.out.println(str.charAt(2));
}
}
5.异常处理方式概述
-
在编写程序时,经常要在可能出现错误的地方加上检测的代码,如进行x/y运算时,要检测分母为0,数据为空,输入的不是数据而是字符等。过多的if-else分支会导致程序的代码加长、臃肿,可读性差。因此采用异常处理机制。
-
Java
异常处理
Java采用的异常处理机制,是将异常处理的程序代码集中在一起, 与正常的程序代码分开,使得程序简洁、优雅,并易于维护。
方式一:
try-catch-finally :自己把狼给解决了
方式二:
throws +
异常类型 :喊人打狼---干不过就报告上级,叫人
package com.atshangguigu.java;
public class ExceptionTest1 {
}
6.处理异常:try-catch方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test07()
{
try{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
@Test
public void test4()
{
String str = "123";
str = "abc";
try{
System.out.println("hello --------0");
int num = Integer.parseInt(str);
System.out.println("hello --------1");
}
catch(NullPointerException e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现空指针异常,别着急呀");
}
catch(NumberFormatException e)//不能够吃错药呀,吃错了不治病!!!
{
//System.out.println("出现数值转换异常,别着急呀");
System.out.println(e.getMessage());
e.printStackTrace();//包含e.getMessage()
}
catch(Exception e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现异常啦,别着急呀");
}
System.out.println("hello --------2");
}
}
7.finally的使用
- 选中-右键-
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
8.编译时的异常与运行时的异常的不同处理方式
- 新的体会:开发中,由于运行时异常是比较常见的,所以我们通常是不针对运行时异常编写try - catch - finally,然而针对于编译时异常我们一定要考虑异常的处理.
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)//不能够进行注释
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
9.异常处理:throws方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{//错误可以一直向上抛,但是main方法是不可以进行数据的抛出的
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
method3();
}
public static void method2() throws IOException
{
method1();
}
public static void method3()
{
try
{
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void method1()
throws FileNotFoundException,IOException{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
System.out.println("哈哈哈哈哈哈");
}
}
10.重写方法异常抛出的规则
11.开发中如何选择哪种方式处理异常
package com.atshangguigu.java1;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest override = new OverrideTest();
override.display(new SubClass());//进行多态,调用子类的函数方法
}
public void display(SuperClass s)
{
try{s.method();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class SuperClass
{
public void method() throws IOException
{
}
}
class SubClass extends SuperClass
{
public void method() throws FileNotFoundException//进行了方法的重写,只能是比父类小的异常
{
}
}
12.手动抛出异常对象
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
- 异常:在Java语言之中,将程序执行中发生的不正常情况称之为"异常"(开发过程之中的语法错误和逻辑错误不是异常)
- Java程序在执行的过程之中所发生的异常事件可以分为两类: Error : Java 虚拟机无法解决的严重问题。如: JVM 系统内部错误、资源耗尽等严重情况。比如: StackOverflowError 和 OOM 。一般不编写针对性的代码进行处理。(严重的问题) Exception: 其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。例如: 空指针访问 试图读取不存在的文件 网络连接中断 数组角标越界
package com.atshangguigu.java;
public class ErrorTest {
public static void main(String[] args) {
//A.Error
//1.栈溢出:java.lang.StackOverflowError
//main(args);
//2.堆溢出:java.lang.OutOfMemoryError: Java heap space
//Integer[] arr = new Integer[1024 * 1024 * 1024];
//B.Excpeption(狭义上的异常)
}
}
3.异常的分类
-
对于这些错误,一般有两种解决方法:一是遇到错误就终止程序的运行。另一种方法是由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。
捕获错误最理想的是在编译期间,但有的错误只有在运行时才会发生。比如:除数为0,数组下标越界等
分类:编译时异常和运行时异常
package com.atshangguigu.java;
public class ExceptionTest {
}
4.常见异常的举例
package com.atshangguigu.java;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionTest {
@Test
public void test07()
{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
//6.ArithmeticException(算数异常)
@Test
public void test06()
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
//5.InputMismatchException
@Test
public void test5()
{
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
scanner.close();
}
//4.NmberFormatException
@Test
public void test4()
{
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//3.ClassCastException
@Test
public void test3()
{
Object obj = new Date();
String Str = (String)obj;
}
//2.IndexOutOfBoundsException
@Test
public void test2()
{
//(1)ArrayIndexOutOfBoundsException
// int[] arr = new int[10];
// System.out.println(arr[10]);
//(2)StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}
//1.NullPointerException
@Test
public void test1()
{
//(1)
// int[] arr = null;
// System.out.println(arr[15]);
//(2)
String str = "abc";
str = null;
System.out.println(str.charAt(2));
}
}
5.异常处理方式概述
-
在编写程序时,经常要在可能出现错误的地方加上检测的代码,如进行x/y运算时,要检测分母为0,数据为空,输入的不是数据而是字符等。过多的if-else分支会导致程序的代码加长、臃肿,可读性差。因此采用异常处理机制。
-
Java
异常处理
Java采用的异常处理机制,是将异常处理的程序代码集中在一起, 与正常的程序代码分开,使得程序简洁、优雅,并易于维护。
方式一:
try-catch-finally :自己把狼给解决了
方式二:
throws +
异常类型 :喊人打狼---干不过就报告上级,叫人
package com.atshangguigu.java;
public class ExceptionTest1 {
}
6.处理异常:try-catch方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test07()
{
try{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
@Test
public void test4()
{
String str = "123";
str = "abc";
try{
System.out.println("hello --------0");
int num = Integer.parseInt(str);
System.out.println("hello --------1");
}
catch(NullPointerException e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现空指针异常,别着急呀");
}
catch(NumberFormatException e)//不能够吃错药呀,吃错了不治病!!!
{
//System.out.println("出现数值转换异常,别着急呀");
System.out.println(e.getMessage());
e.printStackTrace();//包含e.getMessage()
}
catch(Exception e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现异常啦,别着急呀");
}
System.out.println("hello --------2");
}
}
7.finally的使用
- 选中-右键-
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
8.编译时的异常与运行时的异常的不同处理方式
- 新的体会:开发中,由于运行时异常是比较常见的,所以我们通常是不针对运行时异常编写try - catch - finally,然而针对于编译时异常我们一定要考虑异常的处理.
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)//不能够进行注释
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
9.异常处理:throws方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{//错误可以一直向上抛,但是main方法是不可以进行数据的抛出的
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
method3();
}
public static void method2() throws IOException
{
method1();
}
public static void method3()
{
try
{
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void method1()
throws FileNotFoundException,IOException{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
System.out.println("哈哈哈哈哈哈");
}
}
10.重写方法异常抛出的规则
11.开发中如何选择哪种方式处理异常
package com.atshangguigu.java1;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest override = new OverrideTest();
override.display(new SubClass());//进行多态,调用子类的函数方法
}
public void display(SuperClass s)
{
try{s.method();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class SuperClass
{
public void method() throws IOException
{
}
}
class SubClass extends SuperClass
{
public void method() throws FileNotFoundException//进行了方法的重写,只能是比父类小的异常
{
}
}
12.手动抛出异常对象
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
package com.atshangguigu.java;
import java.io.FileInputStream;
import java.util.Date;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionTest {
@Test
public void test07()
{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
//6.ArithmeticException(算数异常)
@Test
public void test06()
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
//5.InputMismatchException
@Test
public void test5()
{
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
scanner.close();
}
//4.NmberFormatException
@Test
public void test4()
{
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//3.ClassCastException
@Test
public void test3()
{
Object obj = new Date();
String Str = (String)obj;
}
//2.IndexOutOfBoundsException
@Test
public void test2()
{
//(1)ArrayIndexOutOfBoundsException
// int[] arr = new int[10];
// System.out.println(arr[10]);
//(2)StringIndexOutOfBoundsException
String str = "abc";
System.out.println(str.charAt(3));
}
//1.NullPointerException
@Test
public void test1()
{
//(1)
// int[] arr = null;
// System.out.println(arr[15]);
//(2)
String str = "abc";
str = null;
System.out.println(str.charAt(2));
}
}
5.异常处理方式概述
-
在编写程序时,经常要在可能出现错误的地方加上检测的代码,如进行x/y运算时,要检测分母为0,数据为空,输入的不是数据而是字符等。过多的if-else分支会导致程序的代码加长、臃肿,可读性差。因此采用异常处理机制。
-
Java
异常处理
Java采用的异常处理机制,是将异常处理的程序代码集中在一起, 与正常的程序代码分开,使得程序简洁、优雅,并易于维护。
方式一:
try-catch-finally :自己把狼给解决了
方式二:
throws +
异常类型 :喊人打狼---干不过就报告上级,叫人
package com.atshangguigu.java;
public class ExceptionTest1 {
}
6.处理异常:try-catch方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test07()
{
try{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
@Test
public void test4()
{
String str = "123";
str = "abc";
try{
System.out.println("hello --------0");
int num = Integer.parseInt(str);
System.out.println("hello --------1");
}
catch(NullPointerException e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现空指针异常,别着急呀");
}
catch(NumberFormatException e)//不能够吃错药呀,吃错了不治病!!!
{
//System.out.println("出现数值转换异常,别着急呀");
System.out.println(e.getMessage());
e.printStackTrace();//包含e.getMessage()
}
catch(Exception e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现异常啦,别着急呀");
}
System.out.println("hello --------2");
}
}
7.finally的使用
- 选中-右键-
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
8.编译时的异常与运行时的异常的不同处理方式
- 新的体会:开发中,由于运行时异常是比较常见的,所以我们通常是不针对运行时异常编写try - catch - finally,然而针对于编译时异常我们一定要考虑异常的处理.
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)//不能够进行注释
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
9.异常处理:throws方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{//错误可以一直向上抛,但是main方法是不可以进行数据的抛出的
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
method3();
}
public static void method2() throws IOException
{
method1();
}
public static void method3()
{
try
{
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void method1()
throws FileNotFoundException,IOException{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
System.out.println("哈哈哈哈哈哈");
}
}
10.重写方法异常抛出的规则
11.开发中如何选择哪种方式处理异常
package com.atshangguigu.java1;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest override = new OverrideTest();
override.display(new SubClass());//进行多态,调用子类的函数方法
}
public void display(SuperClass s)
{
try{s.method();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class SuperClass
{
public void method() throws IOException
{
}
}
class SubClass extends SuperClass
{
public void method() throws FileNotFoundException//进行了方法的重写,只能是比父类小的异常
{
}
}
12.手动抛出异常对象
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
package com.atshangguigu.java;
public class ExceptionTest1 {
}
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test07()
{
try{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
@Test
public void test4()
{
String str = "123";
str = "abc";
try{
System.out.println("hello --------0");
int num = Integer.parseInt(str);
System.out.println("hello --------1");
}
catch(NullPointerException e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现空指针异常,别着急呀");
}
catch(NumberFormatException e)//不能够吃错药呀,吃错了不治病!!!
{
//System.out.println("出现数值转换异常,别着急呀");
System.out.println(e.getMessage());
e.printStackTrace();//包含e.getMessage()
}
catch(Exception e)//不能够吃错药呀,吃错了不治病!!!
{
System.out.println("出现异常啦,别着急呀");
}
System.out.println("hello --------2");
}
}
7.finally的使用
- 选中-右键-
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
8.编译时的异常与运行时的异常的不同处理方式
- 新的体会:开发中,由于运行时异常是比较常见的,所以我们通常是不针对运行时异常编写try - catch - finally,然而针对于编译时异常我们一定要考虑异常的处理.
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)//不能够进行注释
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
9.异常处理:throws方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{//错误可以一直向上抛,但是main方法是不可以进行数据的抛出的
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
method3();
}
public static void method2() throws IOException
{
method1();
}
public static void method3()
{
try
{
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void method1()
throws FileNotFoundException,IOException{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
System.out.println("哈哈哈哈哈哈");
}
}
10.重写方法异常抛出的规则
11.开发中如何选择哪种方式处理异常
package com.atshangguigu.java1;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest override = new OverrideTest();
override.display(new SubClass());//进行多态,调用子类的函数方法
}
public void display(SuperClass s)
{
try{s.method();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class SuperClass
{
public void method() throws IOException
{
}
}
class SubClass extends SuperClass
{
public void method() throws FileNotFoundException//进行了方法的重写,只能是比父类小的异常
{
}
}
12.手动抛出异常对象
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
- 新的体会:开发中,由于运行时异常是比较常见的,所以我们通常是不针对运行时异常编写try - catch - finally,然而针对于编译时异常我们一定要考虑异常的处理.
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.junit.Test;
public class FinallyTest {
@Test
public void test2()
{
FileInputStream fis = null;
try {
File file = new File("Hello.txt");
fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if(fis != null)//不能够进行注释
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
@Test
public void testMethod()
{
int num = method();
System.out.println(num);
}
public int method()
{
try
{
int[] arr = new int[10];
System.out.println(arr[10]);
return 10;
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
return 5;
}
finally
{
System.out.println("晚上我要吃一桶康师傅");
}
}
@Test
public void test1()
{
try
{
int a = 10;
int b = 0;
System.out.println(a/b);
}
catch(ArithmeticException e)
{
e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]);
}
catch(Exception e)
{
e.printStackTrace();
}
//System.out.println("小杨小杨,kaile");
finally
{
System.out.println("康师傅是个好老师");
}
}
}
9.异常处理:throws方式
package com.atshangguigu.java;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try{//错误可以一直向上抛,但是main方法是不可以进行数据的抛出的
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
method3();
}
public static void method2() throws IOException
{
method1();
}
public static void method3()
{
try
{
method2();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public static void method1()
throws FileNotFoundException,IOException{
File file = new File("Hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1)
{
System.out.println((char)data);
data = fis.read();
}
fis.close();
System.out.println("哈哈哈哈哈哈");
}
}
10.重写方法异常抛出的规则
11.开发中如何选择哪种方式处理异常
package com.atshangguigu.java1;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest override = new OverrideTest();
override.display(new SubClass());//进行多态,调用子类的函数方法
}
public void display(SuperClass s)
{
try{s.method();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class SuperClass
{
public void method() throws IOException
{
}
}
class SubClass extends SuperClass
{
public void method() throws FileNotFoundException//进行了方法的重写,只能是比父类小的异常
{
}
}
12.手动抛出异常对象
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
11.开发中如何选择哪种方式处理异常
package com.atshangguigu.java1;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest override = new OverrideTest();
override.display(new SubClass());//进行多态,调用子类的函数方法
}
public void display(SuperClass s)
{
try{s.method();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
class SuperClass
{
public void method() throws IOException
{
}
}
class SubClass extends SuperClass
{
public void method() throws FileNotFoundException//进行了方法的重写,只能是比父类小的异常
{
}
}
12.手动抛出异常对象
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
- [在抛出异常的过程之中]关于异常对象的产生:(1)系统自动生成异常对象(2)手动生成一个异常对象,并且抛出(throw).
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new Exception("您输入的数据是非法的");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
13.如何自定义异常
package com.atshangguigu.java1;
public class MyException extends RuntimeException{
static final long serialVersionUID = -7034897190745766936L;
public MyException()
{
}
public MyException(String msg)
{
super(msg);
}
}
14.异常处理练习基本使用
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
package com.atshangguigu.java1;
public class ReturnExceptionDemo {
static void methodA() {
try {
System.out.println("进入方法A");//1
throw new RuntimeException("制造异常");//3
} finally {
System.out.println("用A方法的finally");//2
}
}
static void methodB() {
try {
System.out.println("进入方法B");//a
return;//c
} finally {
System.out.println("调用B方法的finally");//b
}
}
public static void main(String[] args) {
try {
methodA();
} catch (Exception e) {
System.out.println(e.getMessage());//对应上面的3
}
methodB();
}
}
package com.atshangguigu.java1;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1000);
System.out.println(s);
} catch (Exception e) {
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student
{
private int id;
public void regist(int id) //throws Exception
{
if(id > 0)
{
this.id = id;
}
else
{
// System.out.println("你输入的数据是非法的");
//手动抛出一个异常对象
//throw new RuntimeException("您输入的数据是非法的");
throw new MyException("???");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
15.异常处理练习综合使用
package com.atshangguigu.java2;
public class EcmDef {
public static void main(String[] args) {
try {
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
int result = ecm(i,j);
System.out.println(result);
} catch (NumberFormatException e) {
System.out.println("数据类型不一致");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("缺少命令行参数");
} catch (ArithmeticException e)
{
System.out.println("除0");
}
catch(EcDef e)
{
System.out.println(e.getMessage());
}
}
public static int ecm(int i,int j) throws EcDef
{
if(i < 0||j <0)
{
throw new EcDef("分子或者分母是负数");
}
return i/j;
}
}
16.异常处理章节总结
- throw是生成一个异常对象的一个情况
- throws是进行的异常处理
- 面试题:final finally finalize的三者的区别?
- 类似:throw和throws collection与collections String StringBuffer StringBuilder
- ArrayList和linkedList HashMap和linkedHashMap
- 重写和重载之间的区别
- 结构不相似的:抽象的类 接口 == equals() sleep() wait()
- 对比两种处理方式:try-catch-finally:真正的将异常处理给处理掉了,throws的方式只是将异常抛给了方法的调用者,并没有真正将异常处理掉
- throw表示抛出一个异常类的对象,生成异常对象的过程,声明在方法体内.
- throws属于异常处理的一种方式,声明在方法的声明处.



