Программирование на Java

Особые случаи


Во время исполнения кода могут возникать ситуации, которые почти не описаны в литературе.

Рассмотрим такую ситуацию:

import java.io.*; public class Test {

public Test() { } public static void main(String[] args) { Test test = new Test(); try { test.doFileInput("bogus.file"); } catch (IOException ex) { System.out.println("Second exception handle stack trace"); ex.printStackTrace(); } }

private String doFileInput(String fileName) throws FileNotFoundException,IOException { String retStr = ""; java.io.FileInputStream fis = null; try { fis = new java.io.FileInputStream(fileName); } catch (FileNotFoundException ex) { System.out.println("First exception handle stack trace"); ex.printStackTrace(); throw ex; } return retStr; } }

Результат работы будет выглядеть следующим образом:

java.io.FileNotFoundException: bogus.file (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:64) at experiment.Test.doFileInput(Test.java:33) at experiment.Test.main(Test.java:21) First exception handle stack trace java.io.FileNotFoundException: bogus.file (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:64) at experiment.Test.doFileInput(Test.java:33) at experiment.Test.main(Test.java:21) Second exception handle stack trace

Так как при вторичном возбуждении используется один и тот же объект Exception, стек в обоих случаях будет содержать одну и ту же последовательность вызовов. То есть при повторном возбуждении исключения, если мы используем тот же объект, изменения его параметров не происходит.

Рассмотрим другой пример:

import java.io.*;

public class Test {

public Test() { } public static void main(String[] args) { Test test = new Test(); try { test.doFileInput(); } catch (IOException ex) { System.out.println("Exception hash code " + ex.hashCode()); ex.printStackTrace(); } }


private String doFileInput() throws FileNotFoundException,IOException{ String retStr = ""; java.io.FileInputStream fis = null; try { fis = new java.io.FileInputStream("bogus.file"); } catch (FileNotFoundException ex) { System.out.println("Exception hash code " + ex.hashCode()); ex.printStackTrace(); fis = new java.io.FileInputStream("anotherBogus.file"); throw ex; } return retStr; } }

java.io.FileNotFoundException: bogus.file ( The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:64) at experiment.Test.doFileInput(Test.java:33) at experiment.Test.main(Test.java:21) Exception hash code 3214658

java.io.FileNotFoundException: (The system cannot find the path specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:64) at experiment.Test.doFileInput(Test.java:38) at experiment.Test.main(Test.java:21) Exception hash code 6129586

Несложно заметить, что, хотя последовательность вызовов одна и та же, в вызываемом и вызывающем методах обрабатываются разные объекты исключений.


Содержание раздела