在局部内部类的方法中(比如:show)如果调用局部内部类所声明的方法(如:method)中的局部变量(比如:num)的话,
要求此局部变量声明为final的。
jdk7及之前版本:要求此局部变量显式声明为final
jdk8及以后的版本,可以省略final的声明
public void method() {
//局部变量
final int num =10;
class AA{
public void show() {
// num = 20;
System.out.println(num);
}
}
/*
Error:
java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况错误
一般不编写针对性的代码进行处理
public class ErrorTest {
public static void main(String[] args) {
//1.栈溢出:java.lang.StackOverflowError
// main(args);
//堆溢出:java.lang.OutOfMemoryError
// Integer[] arr = new Integer[102410241024];
}
}
一、异常体系结构
public class ExceptionTest {
//***********以下是编译时异常*****************
@Test
public void test7() {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while(data != -1) {
System.out.print((char)data);
data = fis.read();
}
fis.close();
}
//***********以下上运行时异常*****************
//NullPointerException空指针异常
@Test
public void test1() {
// int[] arr = new int[3];
// System.out.println(arr[3]);
String str = "abc";
str=null;
System.out.println(str.charAt(0));
}
@Test
public void test2() {
//ArrayIndexOutOfBundsException数组脚越界
// int[] arr =new int[10];
// System.out.println(arr[10]);
//StringIndexOutOfBundsException
String str = "abc";
System.out.println(str.charAt(3));
}
//ClassCastException类型转换异常
@Test
public void test3() {
Object obj = new Date();
String str = (String)obj;
}
//NumberFormatException数字格式异常
@Test
public void test4() {
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
//InputMismatchException输入类型不匹配异常
@Test
public void test5(){
Scanner scanner =new Scanner(System.in);
int score = scanner.nextInt();
System.out.println(score);
}
//ArithmeticException算术异常
@Test
public void test6() {
int a =10;
int b = 0;
System.out.println(a/b);
}
}
异常处理的方式二:throws+异常类型
1."throws + 异常类型"写在方法的声明处,指明此方法执行时,可能会抛出的异常类型。
一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对满足throws后异常类型时,就会被抛出。异常代码后续的代码,就不再执行!
2.体会:try-catch-finally:真正的将异常给处理掉了。
throws的方式只是将异常抛给了方法的调用者。并没有真正将异常处理掉。
3.开发中如何选择使用try-catch-finally还是使用throws?
3.1如果父类中被重写的方法没有throws方式处理异常,则子类重写的方法也不能使用throws,意味着如果子类重写的方法中有异常,必须使用try-catch-finally方式处理。
3.2执行方法A中,先后又调用了另外的几个方法,这几个发方法是递进关系执行的。我们建议这几个方法使用throws的方式进行处理。而执行方法A可以考虑使用try-catch-finally方式进行处理。
public class ExceptionTest2{
public static void main(String[] args){
try {
method2();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// method3();
}
public static void method3() {
try {
method2();
}catch (IOException e) {
e.printStackTrace();
}
}
public static void method2() throws IOException{
method1();
}
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.print((char)data);
data = fis.read();
}
fis.close();
System.out.println("haha");
}
}
课后练习题一:
编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除
- 对数据类型不一致(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException、
- 除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
- 提示
- 1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能
- 2)在main()方法中使用异常处理语句进行异常处理。
- 3)在程序中,自定义对应输入负数的异常类(EcDef)。
- 4)运行时接受参数java EcmDef 20 10 //args[0]=“20” args[1]=“10”
- 5)Interger类的static方法parseInt(String s)将s转换成对应int值
- 如:int a = Interger.parseInt(“314”) a=314
编写应用程序EcmDef.java,接收命令行的两个参数,要求不能输入负数,计算两数相除
- 对数据类型不一致(NumberFormatException)、缺少命令行参数(ArrayIndexOutOfBoundsException、
- 除0(ArithmeticException)及输入负数(EcDef 自定义的异常)进行异常处理。
- 提示
- 1)在主类(EcmDef)中定义异常方法(ecm)完成两数相除功能
- 2)在main()方法中使用异常处理语句进行异常处理。
- 3)在程序中,自定义对应输入负数的异常类(EcDef)。
- 4)运行时接受参数java EcmDef 20 10 //args[0]=“20” args[1]=“10”
- 5)Interger类的static方法parseInt(String s)将s转换成对应int值
- 如:int a = Interger.parseInt(“314”) a=314
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;
}
}
自定义异常类
public class EcDef extends Exception{
static final long serialVersionUID = -3387516993124229948L;
public EcDef() {}
public EcDef(String msg) {
super(msg);
}
}