java学习总结——第十一天
2014-04-15 20:31
281 查看
异常:
程序在运行过程中遇到的错误称为异常。
所有异常的父类是:java.lang.Throwable,他有两个重要的分支:Error和Exception。
Throwable
/ \
/ \
Error Exception
\
RuntimeException
Error :虚拟机引发的较严重的错误
Exception:可以通过程序进行处理的异常
java内置了许多表示异常原因的异常类:
NumberFormatException 转换数字异常
NullPointerException 空指针异常
ArithmeticException 算数异常
ArrayIndeOutOfBoundsException 数组下标越界
IllegalArgumentException 方法参数不正确
ClassCastException 类型不匹配
OutofMemoryError 内存溢出
NoClassDefFoundError 找不到类
异常抛出:
try { //嵌套
// byte[] b = new byte[1024 * 1024 * 200];
try {
byte[] b = new byte[1024 * 1024 * 200];
} catch (Exception e) {
// TODO: handle exception
}
//System.out.println("Hello");
} catch (Throwable e) {
if (e instanceof java.lang.OutOfMemoryError) {
System.out.println("内存溢出");
} else {
System.out.println("内存不够!2");
}
//System.out.println("Hello");
}finally{
//最终执行块,一般用于资源的关闭
System.out.println("Hello");
}
自定义的异常:
public void setName(String name) throws RuntimeException { //声明异常
if (name.length() <= 0 || name.length() >= 4) {
throw new RuntimeException(); //抛出异常
}
this.name = name;
}
public static void main(String[] args) {
try {
new Test2().setName("1234");
} catch (Exception e) {
System.out.println("名字长度超出或者是过短");
}
抛出异常综合例题:
public class Test1 {
public static void method1() throws NullPointerException {
try {
System.out.println("0");
throw new NullPointerException();
} finally {
System.out.println("1");
}
}
public static void method2() {
System.out.println("2");
method1();
System.out.println("3");
}
public static void method3()throws NullPointerException{
try{
System.out.println("4");
method2();
System.out.println("5");
}catch(NullPointerException e){
System.out.println("6");
throw e;
}finally{
System.out.println("7");
}
}
public static void main(String[] args) {
method3();
}
}
运行结果:4 2 0 1 6 7(一字一行)。
***抛出异常后先执行finally块然后去处理异常。
异常后的语句不再执行(除fianlly块)。
运行时错误不要求抛异常
Getmessage():得到异常信息
printStackTrace():将此throwable及其追踪输出至标准错误流。
基本数据类型的封装类
Int Integer
float Float
double Double
char Character
long Long
short Short
byte Byte
boolean Boolean
Integer.parseInt(“123”); 将“123”字符串转换为10进制的整数
Interger.parseInt(“123”,16) 将“123”字符串转换为16进制的整数
Integer.toBinaryString(15); 整数转换为2进制数
Integer.toHexString(15); 整数转换为16进制数
Integer.toOctalString(15); 整数转换为8进制数
Integer.valueof(100); 整数类型的数转换为整数类型的字符串
Integer.valueof(“123ABC”,16); 16进制的字符串转换为10进制的整数型的字符串
程序在运行过程中遇到的错误称为异常。
所有异常的父类是:java.lang.Throwable,他有两个重要的分支:Error和Exception。
Throwable
/ \
/ \
Error Exception
\
RuntimeException
Error :虚拟机引发的较严重的错误
Exception:可以通过程序进行处理的异常
java内置了许多表示异常原因的异常类:
NumberFormatException 转换数字异常
NullPointerException 空指针异常
ArithmeticException 算数异常
ArrayIndeOutOfBoundsException 数组下标越界
IllegalArgumentException 方法参数不正确
ClassCastException 类型不匹配
OutofMemoryError 内存溢出
NoClassDefFoundError 找不到类
异常抛出:
try { //嵌套
// byte[] b = new byte[1024 * 1024 * 200];
try {
byte[] b = new byte[1024 * 1024 * 200];
} catch (Exception e) {
// TODO: handle exception
}
//System.out.println("Hello");
} catch (Throwable e) {
if (e instanceof java.lang.OutOfMemoryError) {
System.out.println("内存溢出");
} else {
System.out.println("内存不够!2");
}
//System.out.println("Hello");
}finally{
//最终执行块,一般用于资源的关闭
System.out.println("Hello");
}
自定义的异常:
public void setName(String name) throws RuntimeException { //声明异常
if (name.length() <= 0 || name.length() >= 4) {
throw new RuntimeException(); //抛出异常
}
this.name = name;
}
public static void main(String[] args) {
try {
new Test2().setName("1234");
} catch (Exception e) {
System.out.println("名字长度超出或者是过短");
}
抛出异常综合例题:
public class Test1 {
public static void method1() throws NullPointerException {
try {
System.out.println("0");
throw new NullPointerException();
} finally {
System.out.println("1");
}
}
public static void method2() {
System.out.println("2");
method1();
System.out.println("3");
}
public static void method3()throws NullPointerException{
try{
System.out.println("4");
method2();
System.out.println("5");
}catch(NullPointerException e){
System.out.println("6");
throw e;
}finally{
System.out.println("7");
}
}
public static void main(String[] args) {
method3();
}
}
运行结果:4 2 0 1 6 7(一字一行)。
***抛出异常后先执行finally块然后去处理异常。
异常后的语句不再执行(除fianlly块)。
运行时错误不要求抛异常
Getmessage():得到异常信息
printStackTrace():将此throwable及其追踪输出至标准错误流。
基本数据类型的封装类
Int Integer
float Float
double Double
char Character
long Long
short Short
byte Byte
boolean Boolean
Integer.parseInt(“123”); 将“123”字符串转换为10进制的整数
Interger.parseInt(“123”,16) 将“123”字符串转换为16进制的整数
Integer.toBinaryString(15); 整数转换为2进制数
Integer.toHexString(15); 整数转换为16进制数
Integer.toOctalString(15); 整数转换为8进制数
Integer.valueof(100); 整数类型的数转换为整数类型的字符串
Integer.valueof(“123ABC”,16); 16进制的字符串转换为10进制的整数型的字符串
相关文章推荐
- DCT JAVA
- 如何在64位windows7上同时使用32位和64位的Eclipse
- java
- java通过实体类自动生成ibatis文件
- Java—内部类
- jdk环境变量配置
- Java中的异常
- 深入分析Java ClassLoader原理
- Java利用OpenOffice将word等office文档转换成PDF
- 五家共井
- java继承
- Java中图片压缩处理
- java: session
- 尝试解答java内存问题
- 尝试解答java内存问题
- "Initializing Java Tooling" or "Repository registry initialization" Eclipse
- java 线程
- Spring 3.0参考手册之SpEL
- 初识springMVC
- Struts2提交表单中文乱码问题解决