异常处理**
1. 包装器
1.1 定义:
Java中的基本数据类型不是对象型(引用类型)。但是在程序中有时需要对对象而不是基本数据类
型进行操作。因此,java里提供了一种叫做包装类(wrapper),它能够把基本数据类型包装成对象类型。
1.2 Java中的包装器类有两个主要的作用
1.提供一种机制,将基本值“包装”到对象中,从而使基本值能够包含在为对象而保留的操作中,
或者从带对象返回值的方法中返回。注意,java5增加了自动装箱和拆箱,程序员过去需手工执行的许多 包装操作,现在可以由java自动处理了。
2.为基本值提供分类功能。这些功能大多数于各种转换有关:在基本值和String 对象间相互转 换,在基本值和String
对象之间按不同基数转换,如二进制、八进制和十六进制等。
1.3 基本数据类型及包装类型的对应关系
boolean Boolean
byte Byte char Character double Double float Float int Integer long
Long short Short
2 . 装箱拆箱
自动装箱和拆箱从Java 1.5开始引入,目的是将原始类型值转自动地转换成对应的对象。自动装箱与
拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单 直接。
package cn.zixinyuan;
public class MyVar1 {
public static void main(String[] args) {
int i = 30;
Integer n = 20;//自动装箱
Integer nn = Integer.valueOf("30");
System.out.println(n.intValue());
//拆箱操作
int t = nn;
int t2 = nn.intValue();
}
}
3. 异常概念
异常指的是在程序运行过程中发生的异常事件,通常是由外部问题(如硬件错误、输入错误)所导 致的。在Java等面向对象的编程语言中异常属于对象。
java异常处理:try catch finally throw thorws java.lang.Exception
异常(Exception)都是运行时的。编译时产生的不是异常,而是错误(Error)。
最开始大家都将程序设计导致的错误(Error)不属于异常(Exception)。
但是一般都将Error作为异常的一种,所以异常一般分两类,Error与Exception。
package cn.zixinyuan;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class Excep {
public static void main(String[] args) {
int a = 30;
int b = 0;
System.out.println(a / b);
int[] ns = {1,2,3};
System.out.println(ns.length);
System.out.println(ns[2]);
System.out.println(ns[3]);
String[] ss = {"aa","php"};
show(ss);
show(new String[]{"a","b"});
show(ns);
}
public static void show(String[] sss){
}
public static void show(int[] sss){
}
}
4. 异常处理
try catch finally throw throws try{}catch(){} try{}catch(){}finally{}
try{}catch(){ }catch(){}catch(){} try{}catch(){
}catch(){}catch(){}finally{} final 常量
finally 在异常处理时,代表最终有没有异常,有没有return continue break 都要执行代码段
package cn.zixinyuan;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class Excep1 {
public static void main(String[] args){
System.out.println(testReturn1());
}
public static int testReturn1(){
int i = 1;
try{
i++;
System.out.println("try:" + i);
return i;
} catch(Exception e){
i++;
System.out.println("catch:" + i);
}finally {
i++;
System.out.println("finally:" + i);
}
return i;
}
}
package cn.zixinyuan;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class Excep2 {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.print("\n请输入数字a");
int a = sc.nextInt();
System.out.print("\n请输入数字b");
int b = sc.nextInt();
System.out.printf("%d / %d = %d \n", a, b, a / b);
} catch (Exception e) {
System.out.println("\n输入有误,必须都输入数字,且第二个b不能输入0.");
}
System.out.println("谢谢使用");
}
try
{
Scanner sc = new Scanner(System.in);
System.out.print("\n请输入数字a");
int a = sc.nextInt();
System.out.print("\n请输入数字b");
int b = sc.nextInt();
System.out.printf("%d / %d = %d \n", a, b, a / b);
} catch(
ArithmeticException e)
{
System.out.println("第二个b不能输入0");
} catch(
InputMismatchException e)
{
System.out.println("输入有误,必须都输入数字");
}catch(
Exception e)
{
System.out.println("\n系统未知异常。");
}finally
{
System.out.println("最终要执行的。");
}
}
5 . 声明异常类
package cn.zixinyuan;
/**
* <p>Project: zixinyuan - OracleUtil
*
* @author zixinyuanya [2991228540@qq.com]
* @version 1.0
* @since 17
*/
public class SexException extends Exception{
public SexException() {
super("性别设置错误,必须为男或女");
}
public SexException(String message) { super(message);
}
}