Академический Документы
Профессиональный Документы
Культура Документы
Este control coge las excepciones de tipo Exception; por lo tanto, controla cualquier excepcin. Esto puede ser una puesta en prctica pobre porque usted pierde la informacin valiosa sobre el tipo de excepcin que est siendo lanzada y hace su cdigo menos eficiente. Por consiguiente, ustedes pueden forzar su programa a determinar el tipo de excepcin antes de que esto pueda decidir la mejor estrategia de recuperacin.
Pregunta 03: Hay algo anmalo con este control de excepcin escrito? Compilar este cdigo? try {} catch (Exception e) {} catch (ArithmeticException a) {}
El primer catch coge las excepciones de tipo la Exception; por lo tanto, esto coge cualquier excepcin, incluyendo ArithmeticException. El segundo tratante nunca poda ser alcanzado. Este cdigo no compilar.
Pregunta 04: Relaciona cada situacin en la primera lista con un artculo en la segunda lista.
1. 2. 3.
4.
5.
int[] A; A[0] = 0; El JVM comienza a controlar su programa, pero no puede encontrar las clases de plataforma Java. (Las clases de plataforma Java residen en classes.zip o rt.jar.) Un programa lee un stream y alcanza el final de streammarker. Antes del cierre del stream y despus del alcance del final del streammarker, un programa trata de leer el stream otra vez.
__error __checked exception __compile error __no exception 3 (compile error). El arreglo no esta inicializado, por lo tanto no compilar. 1 (error). 4 (no exception). Cuando se lee un stream se espera un final de streammarker. Se debe de usar excepciones para controlar comportamientos inesperados en el programa. 2 (checked exception).
Pregunta 05:
Aada un mtodo readList a ListOfNumbers.java. Este mtodo debera leer en valores enteros de un archivo, imprimir cada valor, y aadirlos al final del vector. Usted debera controlar todos los errores apropiados. Usted tambin necesitar un archivo de texto que contiene los nmeros a leer .
import java.io.*; import java.util.Vector; public class ListOfNumbers { private Vector<Integer> victor; private static final int SIZE = 10; public ListOfNumbers () { victor = new Vector<Integer>(SIZE); for (int i = 0; i < SIZE; i++) victor.addElement(new Integer(i)); } public void writeList() { PrintWriter out = null; try { System.out.println("Entering try statement"); out = new PrintWriter(new FileWriter("OutFile.txt")); for (int i = 0; i < SIZE; i++) out.println("Value at: " + i + " = " + victor.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } } } }
import java.io.*; import java.util.Vector; public class ListOfNumbers2 { private Vector<Integer> victor; private static final int SIZE = 10; public ListOfNumbers2() { victor = new Vector<Integer>(SIZE); for (int i = 0; i < SIZE; i++) victor.addElement(new Integer(i)); this.readList("infile.txt"); this.writeList(); } public void readList(String fileName) { String line = null; try { RandomAccessFile raf = new RandomAccessFile(fileName, "r"); while ((line = raf.readLine()) != null) { Integer i = new Integer(Integer.parseInt(line)); System.out.println(i); victor.addElement(i); } } catch(FileNotFoundException fnf) { System.err.println("File: " + fileName + " not found."); } catch (IOException io) { System.err.println(io.toString()); } } public void writeList() { PrintWriter out = null; try { out = new PrintWriter(new FileWriter("outfile.txt")); for (int i = 0; i < victor.size(); i++) out.println("Value at: " + i + " = " + victor.elementAt(i)); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); } finally { if (out != null) { System.out.println("Closing PrintWriter"); out.close(); } else { System.out.println("PrintWriter not open"); } }
"+
Pregunta 06: Modifique el mtodo cat siguiente de modo que esto compile:
public static void cat(File file) { RandomAccessFile input = null; String line = null;
public static void cat(File file) { RandomAccessFile input = null; String line = null; try { input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } catch(FileNotFoundException fnf) { System.err.format("File: %s not found%n", file); } catch(IOException e) { System.err.println(e.toString()); } finally { if (input != null) { try { input.close(); } catch(IOException io) { } } } }
try { input = new RandomAccessFile(file, "r"); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (input != null) { input.close(); } }
}
Dudas
otincopau@upao.edu.pe