java声明方法抛出的异常

雨点打透心脏的1/2处 2022-04-06 09:17 376阅读 0赞

java声明方法抛出的异常

TestExceptions.java

  1. import java.io.*;
  2. //异常
  3. public class TestExceptions {
  4. public static void main(String[] args) {
  5. }
  6. void f() {//throws FileNotFoundException , IOException
  7. FileInputStream in = new FileInputStream("myfile.txt");
  8. int b;
  9. b = in.read();
  10. while (b != -1) {
  11. System.out.print((char) b);
  12. b = in.read();
  13. }
  14. }
  15. }

抛出异常

  1. F:\java>javac TestExceptions.java
  2. TestExceptions.java:10: 错误: 未报告的异常错误FileNotFoundException; 必须对其进行捕获或声明以便抛出
  3. FileInputStream in = new FileInputStream("myfile.txt");
  4. ^
  5. TestExceptions.java:12: 错误: 未报告的异常错误IOException; 必须对其进行捕获或声明以便抛出
  6. b = in.read();
  7. ^
  8. TestExceptions.java:15: 错误: 未报告的异常错误IOException; 必须对其进行捕获或声明以便抛出
  9. b = in.read();
  10. ^
  11. 3 个错误
  12. F:\java>

给方法抛出异常

  1. import java.io.*;
  2. //异常
  3. public class TestExceptions {
  4. public static void main(String[] args) {
  5. }
  6. //throwsthrows FileNotFoundException , IOException
  7. void f() throws FileNotFoundException , IOException {
  8. FileInputStream in = new FileInputStream("myfile.txt");
  9. int b;
  10. b = in.read();
  11. while (b != -1) {
  12. System.out.print((char) b);
  13. b = in.read();
  14. }
  15. }
  16. }

编译没有报异常

  1. F:\java>javac TestExceptions.java
  2. F:\java>
  3. import java.io.*;
  4. //异常
  5. public class TestExceptions {
  6. public static void main(String[] args) {
  7. }
  8. //throwsthrows FileNotFoundException , IOException
  9. void f() throws FileNotFoundException , IOException {
  10. FileInputStream in = new FileInputStream("myfile.txt");
  11. int b;
  12. b = in.read();
  13. while (b != -1) {
  14. System.out.print((char) b);
  15. b = in.read();
  16. }
  17. }
  18. //throws IOException
  19. void f2() throws IOException {
  20. //捕捉f()方法中可能出现的异常
  21. try {
  22. f();
  23. } catch (FileNotFoundException e) {
  24. System.out.println(e.getMessage());
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. import java.io.*;
  31. //异常
  32. public class TestExceptions {
  33. public static void main(String[] args) /*throws Exception*/ {
  34. try {
  35. TestExceptions te = new TestExceptions();
  36. te.f2();
  37. } catch(IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. //throwsthrows FileNotFoundException , IOException
  42. void f() throws FileNotFoundException , IOException {
  43. FileInputStream in = new FileInputStream("myfile.txt");
  44. int b;
  45. b = in.read();
  46. while (b != -1) {
  47. System.out.print((char) b);
  48. b = in.read();
  49. }
  50. }
  51. void f2() throws IOException {
  52. /*
  53. 方法一:
  54. //捕捉f()方法中可能出现的异常
  55. try {
  56. f();
  57. } catch (FileNotFoundException e) {
  58. System.out.println(e.getMessage());
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. */
  63. /*
  64. 方法二:
  65. 在f2() 方法后抛出异常f2() throws IOException
  66. */
  67. f();
  68. }
  69. }

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读