我的java学习日记(11)
2014-04-12 23:07
399 查看
Java学习第十一节之异常
一、 异常类型
1、java.lang.NullPointerException(空指针错误)
String s=null;
System.out.println("hello");
s.toString();
//仅会打印hello
System.out.println("java");
2、java.lang.NumberFormatException(数字格式化错误)
Integer.parseInt("123"); //只有int型数据才不会有异常
Integer.parseInt("s");
Integer.parseInt(null);
3、java.lang.ArrayIndexOutOfBoundsException
(数组越界异常)
int[] n={1,2,3};
n[3]=100;
4、java.lang.ClassCastException
(类型转换错误)
Strings=new String("sss");
objectobj=s;
Testt=(Test)obj;
5、java.lang.NoClassDefFoundError
(找不到类,多类的依赖性)
java.lang.NotFoundException
6、java.lang.NoSuchNethodError
(版本差别,1.4以前的版本会报错,但是1.4以后的不会)
7、java.lang.ArithmeticException
(不能被0整除)
8、java.lang.OutOfMemoryError
(内存不够)
二、异常运行
1、异常代码
privateString name;
publicvoid setName(String name)
throws Exception{
if(name.length()<=0|| name.length()>=4){
thrownew Exception();
//抛出异常后,自动终止代码运行
}
this.name=name;
}
publicstatic void main(String[] args){ //方法、构造器等均可抛出异常
try{
newtest1().setName("1233");
}catch(Exceptione){
System.out.println("名字长度不符");
}
}
2、自定义异常
可继承于Throwable、Exception和RuntimeException等。示例如下:
//操作类
publicclass test4 {
public void mm()throwsNameLengthException{
System.out.println("0");
throw new NameLengthException("1");
}
public void mm1()throwsException{
System.out.println("2");
mm();
try{
mm();
}catch(NameLengthException e){
System.out.println("3");
throw e;
}finally{
System.out.println("4");
}
}
public void mm2()throwsException{
System.out.println("5");
mm1();
System.out.println("6");
}
public static voidmain(String[] args)throws Exception{
System.out.println("7");
new test4().mm2();
System.out.println("8");
}
}
//异常类
publicclass NameLengthException
extends RuntimeException{
public NameLengthException(String s) {
super(s);
}
}
3、封装数据类型
数据类型 类
int
Integer
floate Floate
double Double
char Character //1.4版本前区分,之后不以区分
long
Long
byte Byte
boolean
Boolean
代码练习:
publicstaticvoid main(String[] args){
System.out.println(Integer.parseInt("14"));
System.out.println(Integer.parseInt("14",16));
System.out.println(Integer.parseInt("14",8));
System.out.println(Integer.parseInt("1010101011101",2));
System.out.println(Integer.SIZE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.toBinaryString(14));
System.out.println(Integer.toHexString(14));
System.out.println(Integer.toOctalString(14));
}
一、 异常类型
1、java.lang.NullPointerException(空指针错误)
String s=null;
System.out.println("hello");
s.toString();
//仅会打印hello
System.out.println("java");
2、java.lang.NumberFormatException(数字格式化错误)
Integer.parseInt("123"); //只有int型数据才不会有异常
Integer.parseInt("s");
Integer.parseInt(null);
3、java.lang.ArrayIndexOutOfBoundsException
(数组越界异常)
int[] n={1,2,3};
n[3]=100;
4、java.lang.ClassCastException
(类型转换错误)
Strings=new String("sss");
objectobj=s;
Testt=(Test)obj;
5、java.lang.NoClassDefFoundError
(找不到类,多类的依赖性)
java.lang.NotFoundException
6、java.lang.NoSuchNethodError
(版本差别,1.4以前的版本会报错,但是1.4以后的不会)
7、java.lang.ArithmeticException
(不能被0整除)
8、java.lang.OutOfMemoryError
(内存不够)
二、异常运行
1、异常代码
privateString name;
publicvoid setName(String name)
throws Exception{
if(name.length()<=0|| name.length()>=4){
thrownew Exception();
//抛出异常后,自动终止代码运行
}
this.name=name;
}
publicstatic void main(String[] args){ //方法、构造器等均可抛出异常
try{
newtest1().setName("1233");
}catch(Exceptione){
System.out.println("名字长度不符");
}
}
2、自定义异常
可继承于Throwable、Exception和RuntimeException等。示例如下:
//操作类
publicclass test4 {
public void mm()throwsNameLengthException{
System.out.println("0");
throw new NameLengthException("1");
}
public void mm1()throwsException{
System.out.println("2");
mm();
try{
mm();
}catch(NameLengthException e){
System.out.println("3");
throw e;
}finally{
System.out.println("4");
}
}
public void mm2()throwsException{
System.out.println("5");
mm1();
System.out.println("6");
}
public static voidmain(String[] args)throws Exception{
System.out.println("7");
new test4().mm2();
System.out.println("8");
}
}
//异常类
publicclass NameLengthException
extends RuntimeException{
public NameLengthException(String s) {
super(s);
}
}
3、封装数据类型
数据类型 类
int
Integer
floate Floate
double Double
char Character //1.4版本前区分,之后不以区分
long
Long
byte Byte
boolean
Boolean
代码练习:
publicstaticvoid main(String[] args){
System.out.println(Integer.parseInt("14"));
System.out.println(Integer.parseInt("14",16));
System.out.println(Integer.parseInt("14",8));
System.out.println(Integer.parseInt("1010101011101",2));
System.out.println(Integer.SIZE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.toBinaryString(14));
System.out.println(Integer.toHexString(14));
System.out.println(Integer.toOctalString(14));
}
相关文章推荐
- Java菜鸟学习日记11
- 黑马程序员_Java学习日记11_常用类
- java学习日记_11:面向对象之简单的建立一个类。
- Java学习日记11:JSP自定义标签
- 【笨鸟先飞】Java重新学习日记11---创建一个类
- Java学习日记-11 集合(1)
- 黑马程序员——学习日记11 java多线程
- Java学习日记11——构造一个对象的过程
- 黑马程序员—学习java基础3日记
- 黑马程序员--Java学习日记之面向对象(多态,接口和其他关键字)
- JAVA 学习日记(5).JSP,JDBC数据库查询
- Java学习日记之 Java-IO流
- 黑马程序员_Java学习日记第一天-Java基础
- java学习日记_79:集合框架之数据结构的讲解和引出List子类的特点
- java学习日记_81:集合框架之 LinkedList类与Vector类
- 黑马程序员-------学习日记Java基础 第五天
- 【学习日记】java多线程的知识点总结
- java学习日记_41:面向对象之抽象的引入、抽象的特点和抽象成员的特点
- java学习笔记11
- Java菜鸟学习日记36