栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java学习笔记(6)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java学习笔记(6)

QUESTIONS:

1.Throwable的子类包括哪两类,Error类和Exception类的区别:

Throwable子类包括:Error,Exception
Error不是程序需要捕获和进行处理的。当Error发生时,程序将会停止。
当异常发生时,虚拟机系统根据异常的类型,产生相应的异常对象,程序中应对这些异常对象进行相应的处理。

2.checked异常和unchecked异常:
  • Checked 异常: 继承自 Exception 类。代码需要处理 API 抛出的 checked 异常,要么用 try catch 语句,要么直接用 throws 。

  • Unchecked 异常: 也称 RuntimeException,继承自 Exception。代码不需要处理它们的异常也能通过编译。
    checked异常

import java.io.*
class TestScreenIn{
	public static void main(String[] args) throws IOException{
		BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
		String c1;
		int i=0;
		String[] e=new String[10];
		while(i<10){
			c1 = keyin.readLine();
			e[i] = c1;
			i++;
		}
	}
}

unchecked异常

import java.util.*
class TestEmptyStack{
	public static void main(String[] args){
		Stack st =new Stack();
		Object ob = st.pop();
	}
}
3.StackOverflowError和OutOfMemoryError发生情形,原因:

StackOverFlowError:线程申请的栈深度超过允许的最大深度。

OutOfMemoryError: 基本上虚拟机的运行时数据区域(堆,栈,方法区)都会发生,是指虚拟机扩展时无法申请到足够的内存空间。

4.简述异常处理的两种方法:

1.try-catch-finally
try语句含有可能出现异常的语句,可能会抛出一个或多个异常。
catch用于捕获异常。catch在捕获异常时,要和try语句抛出的异常进行比较,相同则进行处理,否则寻找其他catch语句。
finally为语句块的同意出口,为可选部分。

public class NestingException{
	public static void main(String[] args){
		int a,b,c;
		a=67;b=0;
		try{
			c=a/b;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

在当前方法中不处理,throws 异常抛给调用者处理

import java.io.*
class TestScreenIn{
	public static void main(String[] args) throws IOException{
		BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
		String c1;
		int i=0;
		String[] e=new String[10];
		while(i<10){
			c1 = keyin.readLine();
			e[i] = c1;
			i++;
		}
	}
}
5.选取RuntimeException五个子类,编写抛出捕获异常程序:

除0

public class NestingException{
	public static void main(String[] args){
		int a,b,c;
		a=67;b=0;
		try{
			c=a/b;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}

空栈

import java.util.*
class TestEmptyStack{
	public static void main(String[] args){
	try{
		Stack st =new Stack();
		Object ob = st.pop();
		}
		catch(EmptyStackException e){
			System.out.println("XD");
			}
	}
}

空指针

public class TestArray{
		public static void main(String[] args){
		try{
		int[] x;
		System.out.println(x[0]);
		}
		catch(NullPointException e){
		System.out.println("hh");
		}
	
	}
}

越界

public class TestArgs{
	public static void main(String[] args){
	try{
		String foo=args[1];
		System.out.println("foo="+foo);
		}
		catch(IndexOutOfBoundsException e){
		System.out.println("ahhh");
		}
	}
}

读入

import java.io.*
class TestScreenIn{
	public static void main(String[] args){
	try{
		BufferedReader keyin=new BufferedReader(new InputStreamReader(System.in));
		String c1;
		int i=0;
		String[] e=new String[10];
		while(i<10){
			c1 = keyin.readLine();
			e[i] = c1;
			i++;
		}
		catch(IOException e){
			System.out.println("?");
		}
		}
	}
}
6.定义一个异常类:
public class SelfGenerateException extends Exception{
	SelfGenerateException(String msg){
		super(msg);
	}
	static void throwOne()throws SelfGenerateException{
		int a=1;
		if(a==1)
		{throw new SelfGenerateException("a为1");}
	}
	public static void main(String args[]){
	try
	{hrowOne();}
	catch(SelfGenerateException e)
	{e.printStackTrace();}
	}
}
7.throws和throw区别:

throws出现在方法函数头;而throw出现在函数体。
throws表示出现异常的一种可能性,不一定会发生这些异常;throw则是抛出了异常。

public class SelfGenerateException extends Exception{
	SelfGenerateException(String msg){
		super(msg);
	}
	static void throwOne()throws SelfGenerateException{
		int a=1;
		if(a==1)
		{throw new SelfGenerateException("a为1");}
	}
	public static void main(String args[]){
	try
	{hrowOne();}
	catch(SelfGenerateException e)
	{e.printStackTrace();}
	}
}
8.finally作用:

为try-catch-finally统一出口,一般用来执行善后操作。为可选部分,一旦选定,必定执行。

未完待续……

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344949.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号