当前位置: 主页 > JAVA语言

java类型强制转换异常-java强制转换string

发布时间:2023-03-23 16:21   浏览次数:次   作者:佚名

除了 try...catch...捕获异常,我们还可以通过throws声明抛出异常。

当你定义了一个方法时,可以用 throws关键字声明。使用了 throws关键字表明,该方法不处理异常,而是把异常留给它的调用者处理。是不是觉得TA不负责任?

哈哈,看一下demo吧

  1. //该方法通过throws声明了IO异常。

  2. private void readFile() throws IOException {

  3. InputStream is = new FileInputStream("jaywei.txt");

  4. int b;

  5. while ((b = is.read()) != -1) {


  6. }

  7. }

从方法中声明抛出的任何异常都必须使用throws子句。

抛出异常

throw关键字作用是抛出一个 Throwable类型的异常,它一般出现在函数体中。在异常处理中,try语句要捕获的是一个异常对象,其实此异常对象也可以自己抛出。

例如抛出一个 RuntimeException 类的异常对象:

  1. throw new RuntimeException(e);

任何Java代码都可以通过 Java 的throw语句抛出异常。

注意点四、try-catch-finally-return执行顺序

try-catch-finally-return 执行描述

看一个例子

  1. public static void main(String[] args) throws IOException {

  2. System.out.println("result:" + test());

  3. }


  4. private static int test() {

  5. int temp = 1;

  6. try {

  7. System.out.println("start execute try,temp is:"+temp);

  8. return ++temp;

  9. } catch (Exception e) {

  10. System.out.println("start execute catch temp is: "+temp);

  11. return ++temp;

  12. } finally {

  13. System.out.println("start execute finally,temp is:" + temp);

  14. ++temp;

  15. }

  16. }

运行结果:

  1. start execute try,temp is:1

  2. start execute finally,temp is:2

  3. result:2

java null强制转换_java强制转换string_java类型强制转换异常

分析

五、Java异常类的几个重要方法

先来喵一眼异常类的所有方法,如下图:

java类型强制转换异常_java强制转换string_java null强制转换

getMessage

  1. Returns the detail message string of this throwable.

getMessage会返回Throwable的 detailMessage属性,而 detailMessage就表示发生异常的详细消息描述。

举个例子, FileNotFoundException异常发生时,这个 detailMessage就包含这个找不到文件的名字。

getLocalizedMessage

  1. Creates a localized description of this throwable.Subclasses may override this

  2. method in order to produce alocale-specific message. For subclasses that do not

  3. override thismethod, the default implementation returns the same result

  4. as getMessage()

throwable的本地化描述。子类可以重写此方法,以生成特定于语言环境的消息。对于不覆盖此方法的子类,默认实现返回与相同的结果 getMessage()。

getCause

  1. Returns the cause of this throwable or null if thecause is nonexistent or unknown.

返回此可抛出事件的原因,或者java类型强制转换异常,如果原因不存在或未知,返回null。

printStackTrace

java null强制转换_java强制转换string_java类型强制转换异常

  1. Prints this throwable and its backtrace to thestandard error stream.


  2. The first line of output contains the result of the toString() method for

  3. this object.Remaining lines represent data previously recorded by the

  4. method fillInStackTrace().

该方法将堆栈跟踪信息打印到标准错误流。

输出的第一行java类型强制转换异常,包含此对象toString()方法的结果。剩余的行表示,先前被方法fillInStackTrace()记录的数据。如下例子:

  1. java.lang.NullPointerException

  2. at MyClass.mash(MyClass.java:9)

  3. at MyClass.crunch(MyClass.java:6)

  4. at MyClass.main(MyClass.java:3)

六、自定义异常

自定义异常通常是定义一个继承自 Exception 类的子类。

那么,为什么需要自定义异常?

下面是我司自定义异常类的一个简单demo

  1. public class BizException extends Exception {

  2. //错误信息

  3. private String message;

  4. //错误码

  5. private String errorCode;


  6. public BizException() {

  7. }


  8. public BizException(String message, String errorCode) {

  9. this.message = message;

  10. this.errorCode = errorCode;

  11. }


  12. @Override

  13. public String getMessage() {

  14. return message;

  15. }


  16. public void setMessage(String message) {

  17. this.message = message;

  18. }


  19. public String getErrorCode() {

  20. return errorCode;

  21. }


  22. public void setErrorCode(String errorCode) {

  23. this.errorCode = errorCode;

  24. }

  25. }

跑个main方测试一下

  1. public class TestBizException {


  2. public static void testBizException() throws BizException {

  3. System.out.println("throwing BizException from testBizException()");

  4. throw new BizException("100","哥,我错了");

  5. }


  6. public static void main(String[] args) {

  7. try {

  8. testBizException();

  9. } catch (BizException e) {

  10. System.out.println("自己定义的异常");

  11. e.printStackTrace();

  12. }

  13. }

  14. }

运行结果:

  1. exception.BizException: 100

  2. throwing BizException from testBizException()

  3. 自己定义的异常

  4. at exception.TestBizException.testBizException(TestBizException.java:7)

  5. at exception.TestBizException.main(TestBizException.java:12)

七、Java7 新的 try-with-resources语句

try-with-resources,是Java7提供的一个新功能,它用于自动资源管理。

java null强制转换_java强制转换string_java类型强制转换异常

在 try-with-resources出现之前

  1. try{

  2. //open resources like File, Database connection, Sockets etc

  3. } catch (FileNotFoundException e) {

  4. // Exception handling like FileNotFoundException, IOException etc

  5. }finally{

  6. // close resources

  7. }

Java7, try-with-resources出现之后,使用资源实现

  1. try(// open resources here){

  2. // use resources

  3. } catch (FileNotFoundException e) {

  4. // exception handling

  5. }

  6. // resources are closed as soon as try-catch block is executed.

Java7使用资源demo

  1. public class Java7TryResourceTest {

  2. public static void main(String[] args) {

  3. try (BufferedReader br = new BufferedReader(new FileReader(

  4. "C:/jaywei.txt"))) {

  5. System.out.println(br.readLine());

  6. } catch (IOException e) {

  7. e.printStackTrace();

  8. }

  9. }

  10. }

使用了 try-with-resources的好处

八、异常链

我们常常会想要在捕获一个异常后抛出另一个异常,并且希望把原始异常的信息保存下来,这被称为异常链。

throw 抛出的是一个新的异常信息,这样会导致原有的异常信息丢失。在JDk1.4以前,程序员必须自己编写代码来保存原始异常信息。现在所有 Throwable 子类在构造器中都可以接受一个 cause(异常因由) 对象作为参数。

这个 cause就用来表示原始异常,这样通过把原始异常传递给新的异常,使得即使当前位置创建并抛出了新的异常,也能通过这个异常链追踪到异常最初发生的位置。

使用方式如下:

  1. public class TestChainException {


  2. public void readFile() throws MyException{

  3. try {

  4. InputStream is = new FileInputStream("jay.txt");

  5. Scanner in = new Scanner(is);

  6. while (in.hasNext()) {

  7. System.out.println(in.next());

  8. }

  9. } catch (FileNotFoundException e) {

  10. //e 保存异常信息

  11. throw new MyException("文件在哪里呢", e);

  12. }

  13. }


  14. public void invokeReadFile() throws MyException{

  15. try {

  16. readFile();

  17. } catch (MyException e) {

  18. //e 保存异常信息

  19. throw new MyException("文件找不到", e);

  20. }

  21. }


  22. public static void main(String[] args) {

  23. TestChainException t = new TestChainException();

  24. try {

  25. t.invokeReadFile();

  26. } catch (MyException e) {

  27. e.printStackTrace();

  28. }

  29. }


  30. }


  31. //MyException 构造器

  32. public MyException(String message, Throwable cause) {

  33. super(message, cause);

  34. }

运行结果:

java强制转换string_java null强制转换_java类型强制转换异常

java强制转换string_java null强制转换_java类型强制转换异常

我们可以看到异常信息有保存下来的,如果把cause(也就是FileNotFoundException 的e)去掉呢,看一下运行结果:

java类型强制转换异常_java null强制转换_java强制转换string

可以发现,少了 Throwablecause,原始异常信息不翼而飞了。

九、异常匹配

抛出异常的时候,异常处理系统会按照代码的书写顺序找出"最近"的处理程序。找到匹配的处理程序之后,它就认为异常将得到处理,然后就不再继续查找。

查找的时候并不要求抛出的异常同处理程序的异常完全匹配。派生类的对象也可以配备其基类的处理程序

看demo

  1. package exceptions;

  2. //: exceptions/Human.java

  3. // Catching exception hierarchies.


  4. class Annoyance extends Exception {}

  5. class Sneeze extends Annoyance {}


  6. public class Human {

  7. public static void main(String[] args) {

  8. // Catch the exact type:

  9. try {

  10. throw new Sneeze();

  11. } catch(Sneeze s) {

  12. System.out.println("Caught Sneeze");

  13. } catch(Annoyance a) {

  14. System.out.println("Caught Annoyance");

  15. }

  16. // Catch the base type:

  17. try {

  18. throw new Sneeze();

  19. } catch(Annoyance a) {

  20. System.out.println("Caught Annoyance");

  21. }

  22. }

  23. }

运行结果:

java类型强制转换异常_java null强制转换_java强制转换string

catch(Annoyance a)会捕获Annoyance以及所有从它派生的异常。捕获基类的异常,就可以匹配所有派生类的异常

  1. try {

  2. throw new Sneeze();

  3. } catch(Annoyance a) {

  4. } catch(Sneeze s) { //这句编译器会报错,因为异常已由前面catch子句处理

  5. }

十、Java常见异常NullPointerException

空指针异常,最常见的一个异常类。简言之,调用了未经初始化的对象或者是不存在的对象,就会产生该异常。

ArithmeticException

java null强制转换_java强制转换string_java类型强制转换异常

算术异常类,程序中出现了除数为0这样的运算,就会出现这样的异常。

ClassCastException

类型强制转换异常,它是JVM在检测到两个类型间转换不兼容时引发的运行时异常。

ArrayIndexOutOfBoundsException

数组下标越界异常,跟数组打交道时,需要注意一下这个异常。

FileNotFoundException

文件未找到异常,一般是要读或者写的文件,找不到,导致该异常。

SQLException

操作数据库异常,它是Checked Exception(检查异常);

IOException

IO异常,一般跟读写文件息息相关,它也是Checked Exception(检查异常)。平时读写文件,记得IO流关闭!

NoSuchMethodException

方法未找到异常

NumberFormatException

字符串转换为数字异常

总结

这个总结独辟蹊径,以几道经典异常面试题结束吧,以帮助大家复习一下,嘻嘻。