首页 > 编程语言 >java面试题-列举常见的异常

java面试题-列举常见的异常

时间:2023-02-28 10:06:48浏览次数:33  
标签:面试题 java string FileNotFoundException ClassNotFoundException 列举 异常 String


面试中经常会被问到,列举几种常见异常。怎么能被这个难倒呢?
下面随便列举些,以及触发例子。

文章目录

  • ​​NullPointerException​​
  • ​​ArithmeticException​​
  • ​​NumberFormatException​​
  • ​​StringIndexOutOfBoundsException​​
  • ​​ArrayIndexOutOfBoundsException​​
  • ​​IllegalArgumentException​​
  • ​​非运行时异常​​
  • ​​ClassNotFoundException​​
  • ​​FileNotFoundException​​
  • ​​扩展​​

NullPointerException

空指针异常

String string = null;
string.length();

ArithmeticException

算术异常

int n=0/0;

顺便拓展下: 1.0/0 会报异常吗?

double d=1.0/0;

不会报异常,返回值是Infinity。

NumberFormatException

数字格式化异常

Double.valueOf("");

StringIndexOutOfBoundsException

字符串下标越界异常

String string="";
string.charAt(2);

ArrayIndexOutOfBoundsException

数组下标越界异常

//        String[] args={}; // main方法中不用这一句
System.out.println(args[5]);

IllegalArgumentException

参数不合法异常

Assert.hasText(null,"用户名为必填");

非运行时异常

ClassNotFoundException

类找不到异常

try {
Class<?> classObject = Class.forName("你找不到个类");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

FileNotFoundException

文件找不到异常

try {
FileInputStream asfasf = new FileInputStream("桃花岛");
} catch (FileNotFoundException e) {
e.printStackTrace();
}

扩展


标签:面试题,java,string,FileNotFoundException,ClassNotFoundException,列举,异常,String
From: https://blog.51cto.com/u_7341513/6090152

相关文章

  • java使用gzip压缩和解压
    代码如下:publicclassGZipUtils{publicstaticfinalintBUFFER=1024;publicstaticfinalStringEXT=".gz";publicstaticvoidmain(String[]args)......
  • java使用commons-lang3
    pom.xml中添加<!--https://mvnrepository.com/artifact/org.apache.commons/commons-lang3--><dependency><groupId>org.apache.commons</groupId><artifactId>co......
  • java常用系统属性System.getProperties().getProperty()
    用法例子:System.out.println(System.getProperties().getProperty("user.home"));常用属性列表:属性含义java.versionJava运行时环境版本java.vendorJava运行时环境供应商jav......
  • java8 flatmap的使用
    Useruser=newUser(“[email protected]”,“1234”);user.setPosition(“Developer”);Stringposition=Optional.ofNullable(user).flatMap(u->u.getPosition()).......
  • java中输出方框(未知字符)
    这说明是空白字符,注意不是空字符串,也不是null。他是unicode中的\u0000也就是NULL.哪些场景会出现该情况?char数组中有未设定的字符,会当做空白字符来打印publicstaticvo......
  • java使用nio多线程读文件
    模拟多线程nio读取文件,并输出,output方法自己补一下。ReadFile代码:publicclassReadFileextendsObservable{privateintbufSize=1024;//换行符privateb......
  • java-jdbc
    0、简介Java数据库连接,(JavaDatabaseConnectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。J......
  • java网络编程-客户端和服务器
    基于java.net包,实现一个简单的服务端和客户端,客户端只管发,服务端只管收缺点:服务端只能处理一个客户端的请求,因为服务端是单线程的。一次只能与一个客户端进行消息通信服......
  • Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is j
    Youmightencounterthe“Failedtostartbean'documentationPluginsBootstrapper';nestedexceptionisjava.lang.NullPointerException”errorwhileupgradingS......
  • JavaFX 学习记录
    使用JavaFX时一些奇怪的问题继承自Application类的构造函数会被执行两次先看代码://FXTestMain.javaimportjavafx.application.Application;importjavafx.stage......