• 异常处理


                                                                      异常处理

     

    Java中的异常类

    Java中 异常对象分为两大类     Error和Exception

    Exception 大致可以分为三类:有关I/O的IOException 有关运行时的异常RuntimeException以及其他类

    RuntimeException异常是由于程序编写过程中的不周全的代码引起的    RuntimeException异常的原因包括如下几种

    1  错误的类型转换

    2  数组越界访问

    3  数学计算错误

    4  试图访问一个空对象

    引起IOException异常的原因包括

    1  试图从文件结尾处读取信息

    2  试图打开一个不存在的或者格式错误的URL

    其他比较常见的异常

    1  用Class.forName()来初始化一个类的时候 字符串参数对应的类不存在

    数学计算异常 (当用户输入一个0来作为除数时,就会报错)

     1 public class ExceptionExam {
     2    public static void main(String []args) {
     3 
     4          int a,b;
     5         double c;
     6         a = Integer.paresInt(args[0]);
     7         b = Integer.paresInt(args[1]);
     8 
     9        c = a/b;
    10 
    11       System.out.println(a+"/"+b+"="+c);
    12 
    13    }
    14 
    15   }

    处理Exception

    常见异常

    1  ArithmeticException  数学计算异常

    2  ArrayIndexOutOfBourbonsException   数组越界异常

    3  NullpointerException  空指针异常

    4  NegativeArraySizeException   负 数组长度异常

    5  ClassCastException 造型异常

    6  IllgalArgumentException   非法参数数值异常

    7  IllegalStateException   对象状态异常 如对未初始化的对象调用方法

    8 UnsuppportedOperationException   对象不支持的操作异常 如调用方法名 方法参数写错等

    通过try-catch-finally 来处理异常

     1 try{
     2   //可能会抛出特定异常的代码
     3 }[catch(MyExceptionType    myException){
     4 
     5     //如果myException被抛出 则执行这段代码
     6 }catch(Exception otherException){
     7     //如果另外的异常otherException被抛出 则执行这段代码
     8 
     9 }] [finally{
    10 
    11     //无条件执行的语句
    12 
    13 }】

    对于异常处理语句可能为下面三种中的一种

    try-catch[-catch...]
    
    try= catch[-catch...]-finally
    
    try-finally

    2017-07-05

  • 相关阅读:
    安装django(使用国内镜像)
    adb工具安装与配置
    mysql
    Charles抓包(iOS的http/https请求)
    selenium+python第一个自动化脚本
    selenium+python环境搭建
    python,你好!
    tjkd-html
    asp
    uglifyjs压缩批处理
  • 原文地址:https://www.cnblogs.com/liuhuaabcp/p/7123695.html
Copyright © 2020-2023  润新知