• 常见异常的解决办法


    常见的异常类型

    • 运行时异常RuntimeException

    1. 算术异常ArithmeticException
    2. 空指针异常NullPointException
    3. 类型转换异常 ClassCastException
    4. 数组下标越界 ArrayIndexOutOfBoundsException
    5. 期望的数据类型与实际输入类型不匹配InputMismatchException

    代码如下:

     1 import java.util.Scanner;
     2 
     3 
     4 public class TestException {
     5 public static void main(String[] args) {
     6     int b=0;
     7     if(b!=0){
     8         int result=1/b;
     9     }else{
    10         System.out.println("对不起除数不为0");
    11     }
    12     //(2)空指针异常,没有创建对象(new)而调用了对象的属性或者方法
    13     String str=null;
    14     if(str!=null){
    15     System.out.println(str.length());
    16 }
    17     //(3)类型转换异常ClassCastException
    18     Animal dog=new Dog();//向上类型转换
    19     if(dog instanceof Cat){//向下类型转换,转换成真实的子类对象
    20     Cat c=(Cat)dog;
    21     }
    22     //(4)数组下标越界ArrayIndexOutOfBounsException
    23     int []arr={11,22,33};
    24     int index=3;
    25     if(index<arr.length-1){
    26     System.out.println(arr[index]);
    27     }else{
    28         System.out.println("数组下标超出了边界");
    29     }
    30     //(5)期望的数据类型与输入的数据类型不相匹配InputMismatchException
    31     Scanner scanner = new Scanner(System.in);
    32     if(scanner.hasNextInt()){
    33     int a=scanner.nextInt();
    34     System.out.println(a);
    35     }else{
    36         System.out.println("对不起,输入的类型与期望的类型不匹配");
    37     }
    38     
    39     }
    40 
    41 }
    42 class Animal{}
    43 class Dog extends Animal{
    44     
    45 }
    • 检查时异常Checked Exception

    1. SQLException :当使用 JDBC 与数据源(在本文中的数据源表示我们实际使用的数据库)进行交互的时候遇见错误的时候,将会抛出名为 SQLException 的异常。一个 SQLException 的异常里面包含以下信息,用于帮助我们更好的定位错误。
    2. IOException   输入输出异常。java内部数据的传输都是通过流,或者byte来进行传递的。就行一个文本文件。你可以通过in流写入到java中,同时也可以通过out流从java(计算机内存中)返还给具体的文件。
    3. ParseException:ParseException是一个异常类,表示解析异常,这里用import引入这个类,比如日期或者字符串格式化会抛出这个异常。

       

    Checked Exception必须做处理,处理方式有两种

    1. throws向上声明
    2. try-catch-finally
     1 import java.io.File;
     2 import java.io.IOException;
     3 
     4 
     5 public class TestCheckedException {
     6 
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9        //检查时异常,N多个程序在编译期间必须处理的异常的总称
    10         File f=new File("D:\a.txt");
    11         try {
    12             f.createNewFile();
    13         } catch (IOException e) {
    14             // TODO Auto-generated catch block
    15             e.printStackTrace();
    16         }//在d盘创建名为a.txt的文件
    17     }
    18 
    19 }

     自定义异常

    1. 为什么需要自定义异常

    在程序中,可能会遇到任何标准异常类都没有充分的描述清楚问题,这种情况下可以创建自己的异常类。

             2.自定义异常的步骤

    • 继承Exception或者RuntimeException
    • 定义构造方法
    • 使用异常
     1 package com.wq;
     2 
     3 public class SexException extends Exception{
     4 
     5     public SexException() {
     6         super();
     7         // TODO Auto-generated constructor stub
     8     }
     9 
    10     public SexException(String message) {
    11         super(message);
    12         // TODO Auto-generated constructor stub
    13     }
    14 
    15 }
     1 package com.wq;
     2 
     3 import java.util.Scanner;
     4 
     5 public class TestSexException {
     6 
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9  Scanner scanner = new Scanner(System.in);
    10  System.out.println("请输入你的性别:");
    11  String gender=scanner.nextLine();
    12  if("".equals(gender)||"".equals(gender)){
    13      System.out.println("信息录入正确");
    14  }else{
    15      try {
    16         throw new SexException("性别只能是男或者女");
    17     } catch (SexException e) {
    18         // TODO Auto-generated catch block
    19         e.printStackTrace();
    20         System.err.println(e.getMessage());
    21     }
    22  }
    23     }
    24 
    25 }

    常见的SQLException异常(摘自他人)

    ORA-00904:  invalid column name 无效列名

    ORA-00942:  table or view does not exist 表或者视图不存在

    ORA-01400:  cannot insert NULL into () 不能将空值插入

    ORA-00936: 缺少表达式

    ORA-00933: SQL 命令未正确结束

    ORA-01722: 无效数字:(一般可能是企图将字符串类型的值填入数字型而造成)

    ORA-06530:  ACCESS_INTO_NULL 
          Your program attempts to assign values to the attributes of an uninitialized (atomically  null) object.
    企图将值写入未初化对象的属性

    ORA-06592:  CASE_NOT_FOUND
         None of th  choice  in the WHEN clauses of a CASE statement is selected,  and there is no  ELSE clause.
    case语句格式有误,没有分支语句

    ORA-06531:  COLLECTION_IS_NULL
         Your program attempts to apply collection methods othe  than EXIST  to an uninitialized(atomically  null) nested table or varray, or th  program attempts to assign values to the elements of an  uninitialized nested table  or  varray.
    企图将集合填入未初始化的嵌套表中

    ORA-06511:  CURSOR_ALREADY_OPEN
         Your program attempts to open an already open cursor. A cursor must be closed before it can  be reopened.  A cursor FOR loop automatically opens the cursor to which it refers. So, your  program cannot open that cursor inside  the  loop.
    企图打开已经打开的指针.指针已经打开,要再次打开必须先关闭.

    ORA-00001:  DUP_VAL_ON_INDEX
         Your program attempts to store duplicate values in a database column that is constrained by  a unique index.
    数据库字段存储重复,主键唯一值冲突

    ORA-01001:  INVALID_CURSOR 无效指针
        Your program attempts an illegal cursor operation such as closing an unopened cursor.
    非法指针操作,例如关闭未打开的指针

    ORA-01722:  INVALID_NUMBER 无效数字
         In a SQL statement, the conversion of a character string into a number fails because the  string does not represent a valid number. (In  procedural statements, VALUE_ERROR is raised.) This exception is also raised when the LIMIT-clause expression in a bulk FETCH  statement does not evaluate to a positive number.
    在sql语句中,字符数字类型转换错误,无法将字符串转化成有效数字.此错误也可能因为在limit从句表达式中fetch语句无法对应指定数字

    ORA-01017:  LOGIN_DENIED 拒绝访问
         Your program attempts to log on to Oracle with an invalid username and/or password.
    企图用无效的用户名或密码登录oracle

    ORA-01403:  NO_DATA_FOUND  无数据发现
         A SELECT INTO statement returns no rows, or your program references a deleted element in a  nested table  or an  uninitialized  element  in  an  index-by  table.  SQL  aggregate  functions  such  as  AVG  and  SUM  always  return  a value or a null.  So, a SELECT INTO statement that calls an aggregate function  never  raises  NO_DATA_FOUND. The FETCH statement is  expected to return no rows eventually, so when that happens, no exception is raised.
      
    ORA-01012:  NOT_LOGGED_ON   未登录
         Your program issues  a database call without being connected to Oracle.
    程序发送数据库命令,但未与oracle建立连接
      
    ORA-06501:  PROGRAM_ERROR  程序错误
         PL/SQL  has  an  internal  problem.
    pl/sql系统问题
      
    ORA-06504:  ROWTYPE_MISMATCH  行类型不匹配
        The  host  cursor  variable  and  PL/SQL  cursor  variable  involved  in  an  assignment  have  incompatible  return  types. 
    For example, when an open host cursor variable is passed to a stored  subprogram, the return types of the actual and formal parameters must be compatible. 
      
    ORA-30625:  SELF_IS_NULL  
         Your  program  attempts  to  call  a  MEMBER  method  on  a  null  instance.  That  is,  the  built-in  parameter  SELF (which  is  always  the  first  parameter  passed  to  a  MEMBER  method)  is  null.
      
    ORA-06500:  STORAGE_ERROR  存储错误
         PL/SQL runs out of memory  or memory has been corrupted.
    PL/SQL运行内存溢出或内存冲突
          
    ORA-06533:  SUBSCRIPT_BEYOND_COUNT   子句超出数量
          Your program references a nested table or varray element using an index number larger than the number of elements in  the collection.
      
    ORA-06532:  SUBSCRIPT_OUTSIDE_LIMIT   子句非法数量
    Your program references a nested table or varray element using an index number (-1  for  example)  that  is outside  the  legal  range.
      
    ORA-01410:  SYS_INVALID_ROWID   无效的字段名
       The conversion  of  a  character  string  into  a  universal  rowid  fails  because  the  character  string  does  not represent  a  valid  rowid. 
      
    ORA-00051:  TIMEOUT_ON_RESOURCE    资源等待超时
    A time-out occurs while Oracle is waiting for a resource. 
      
    ORA-01422:  TOO_MANY_ROWS    返回超过一行
    A SELECT INTO statement returns more  han one row.
      
    ORA-06502:  VALUE_ERROR   值错误
    An arithmetic, conversion, truncation, or size-constraint error occurs. For example, when  your program selects a column value into a character  variable, if the value is  longer  than  the  declared  length  of  the  variable, PL/SQL aborts the assignment and raises VALUE_ERROR.  In procedural statements,  VALUE_ERROR  is raised if the conversion of a character string into a number fails.  (In SQL statements, INVALID_NUMBER is raised.) 
      
    ORA-01476:  ZERO_DIVIDE  除0错误
    Your program attempts to divide a number by zero. 

  • 相关阅读:
    Prometheus实现微信邮件钉钉报警
    产品需求文档和原型
    各类数据集
    redis与mysql数据同步
    hadoop hbase hive spark对应版本
    Redis集群的搭建
    mysql数据库数据与redis同步
    企业级Zabbix监控实战(一)
    mysql实现高可用架构之MHA
    04-爬取单个英雄联盟英雄的符文图片
  • 原文地址:https://www.cnblogs.com/wq-9/p/10301378.html
Copyright © 2020-2023  润新知