⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 answers to questions and exercises exceptions.htm

📁 用java 编写的十分基础的几个简单类
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0079)http://java.sun.com/docs/books/tutorial/essential/exceptions/QandE/answers.html -->
<HTML><HEAD><TITLE>Answers to Questions and Exercises: Exceptions</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JavaScript><!-- hidefunction openWin(term) {  url="../../../information/glossary.html#" + term;  myWin= window.open(url, "Glossary",              "width=400,height=150,scrollbars=yes,status=no,toolbar=no,menubar=no");  myWin.focus();}//--></SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY link=#000099 bgColor=#ffffff><B><FONT size=-1>The Java</FONT><SUP><FONT 
size=-2>TM</FONT></SUP> <FONT size=-1>Tutorial</FONT></B> <BR>
<TABLE width=550 summary=layout>
  <TBODY>
  <TR>
    <TD vAlign=center align=left><A 
      href="http://java.sun.com/docs/books/tutorial/essential/exceptions/QandE/questions.html" 
      target=_top><IMG height=26 alt="Previous Page" 
      src="Answers to Questions and Exercises Exceptions.files/PreviousArrow.gif" 
      width=26 align=center border=0></A> <A 
      href="http://java.sun.com/docs/books/tutorial/essential/TOC.html#exceptions" 
      target=_top><IMG height=26 alt="Lesson Contents" 
      src="Answers to Questions and Exercises Exceptions.files/TOCIcon.gif" 
      width=26 align=center border=0></A> </TD>
    <TD vAlign=center align=middle><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/index.html" 
      target=_top>Start of Tutorial</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/essential/index.html" 
      target=_top>Start of Trail</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html" 
      target=_top>Start of Lesson</A> </FONT></TD>
    <TD vAlign=center align=right><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/search.html" 
      target=_top>Search</A> <BR><A 
      href="http://java.sun.com/docs/books/tutorial/forms/sendusmail.html">Feedback 
      Form</A> </FONT></TD></TR></TBODY></TABLE><IMG height=8 alt="" 
src="Answers to Questions and Exercises Exceptions.files/blueline.gif" width=550 
align=bottom NATURALSIZEFLAG="3"> <BR><FONT size=-1><B>Trail</B>: Essential Java 
Classes <BR><B>Lesson</B>: Handling Errors with Exceptions </FONT>
<H2>Answers to Questions and Exercises: Exceptions </H2>
<BLOCKQUOTE></BLOCKQUOTE>
<H3>Questions</H3>
<BLOCKQUOTE>
  <OL>
    <LI><FONT color=red>Question:</FONT> Is the following code legal? 
    <BLOCKQUOTE><PRE>try {
    ...
} finally {
    ...
}
</PRE></BLOCKQUOTE><FONT color=green>Answer:</FONT> Yes, it's legal. A 
    <CODE>try</CODE> statement does not have to have a <CODE>catch</CODE> 
    statement if it has a <CODE>finally</CODE> statement. If the code in the 
    <CODE>try</CODE> statement has multiple exit points and no associated 
    <CODE>catch</CODE> clauses, the code in the <CODE>finally</CODE> statement 
    is executed no matter how the <CODE>try</CODE> block is exited. 
    <P></P>
    <LI><FONT color=red>Question:</FONT> What exception types can be caught by 
    the following handler? 
    <BLOCKQUOTE><PRE>catch (Exception e) {
    ...
}
</PRE></BLOCKQUOTE>What is wrong with using this type of exception handler? 
    <P><FONT color=green>Answer:</FONT> This handler catches exceptions of type 
    <CODE>Exception</CODE>; therefore, it catches any exception. This can be a 
    poor implementation because you are losing valuable information about the 
    type of exception being thrown and making your code less efficient. As a 
    result, the runtime system is forced to determine the type of exception 
    before it can decide on the best recovery strategy. 
    <P></P>
    <LI><FONT color=red>Question:</FONT> What exceptions can be caught by the 
    following handler? 
    <BLOCKQUOTE><PRE>...
} catch (Exception e) {
    ...
} catch (ArithmeticException a) {
    ...
}
</PRE></BLOCKQUOTE>Is there anything wrong with this exception handler as 
    written? Will this code compile? 
    <P><FONT color=green>Answer:</FONT> This first handler catches exceptions of 
    type <CODE>Exception</CODE>; therefore, it catches any exception, including 
    <CODE>ArithmeticException</CODE>. The second handler could never be reached. 
    This code will not compile. 
    <P></P>
    <LI><FONT color=red>Question:</FONT> Match each situation in the first 
    column with an item in the second column. 
    <TABLE border=1>
      <TBODY>
      <TR>
        <TD vAlign=top>
          <OL type=a>
            <LI><CODE>int[] A; <BR>A[0] = 0;</CODE> 
            <P></P>
            <LI>The Java VM starts running your program, but the VM can抰 find 
            the Java platform classes. (The Java platform classes reside in 
            <CODE>classes.zip</CODE> or <CODE>rt.jar</CODE>.) 
            <P></P>
            <LI>A program is reading a stream and reaches the end of stream 
            marker. 
            <P></P>
            <LI>Before closing the stream and after reaching the end of stream 
            marker, a program tries to read the stream again. </LI></OL>
        <TD vAlign=top>
          <OL type=1>
            <LI>error 
            <P></P>
            <LI>checked exception 
            <P></P>
            <LI>runtime exception 
            <P></P>
            <LI>no exception </LI></OL></TR></TBODY></TABLE>
    <P><FONT color=green>Answer:</FONT> 
    <OL type=a>
      <LI>3 (runtime exception). 
      <LI>1 (error). 
      <LI>4 (no exception). When you read a stream, you expect there to be an 
      end of stream marker. You should use exceptions to catch unexpected 
      behavior in your program. 
      <LI>2 (checked exception). </LI></OL></LI></OL></BLOCKQUOTE>
<H3>Exercises</H3>
<BLOCKQUOTE>
  <OL>
    <LI><FONT color=red>Exercise:</FONT> Add a <CODE>readList</CODE> method to 
    <A 
    href="http://java.sun.com/docs/books/tutorial/essential/exceptions/example/ListOfNumbers.java" 
    target=_blank><FONT 
    color=#bb000f><CODE><CODE>ListOfNumbers.java</CODE></CODE></FONT></A><A 
    href="http://java.sun.com/docs/books/tutorial/essential/exceptions/example/ListOfNumbers.java" 
    target=_blank><IMG height=11 alt=" (in a .java source file)" 
    src="Answers to Questions and Exercises Exceptions.files/sourceIcon.gif" 
    width=11 align=absMiddle border=0></A>. This method should read in 
    <CODE>int</CODE> values from a file, print each value, and append them to 
    the end of the vector. You should catch all appropriate errors. You will 
    also need a text file containing numbers to read in. 
    <P><FONT color=green>Answer:</FONT> See <CODE><A 
    href="http://java.sun.com/docs/books/tutorial/essential/exceptions/example-1dot1/ListOfNumbers2.java" 
    target=_blank><FONT 
    color=#bb000f><CODE><CODE>ListOfNumbers2.java</CODE></CODE></FONT></A><A 
    href="http://java.sun.com/docs/books/tutorial/essential/exceptions/example-1dot1/ListOfNumbers2.java" 
    target=_blank><IMG height=11 alt=" (in a .java source file)" 
    src="Answers to Questions and Exercises Exceptions.files/sourceIcon.gif" 
    width=11 align=absMiddle border=0></A>.</CODE></P>
    <P></P>
    <LI><FONT color=red>Exercise:</FONT> Modify the following <CODE>cat</CODE> 
    method so that it will compile: 
    <BLOCKQUOTE><PRE>public static void cat(File named) {
    RandomAccessFile input = null;
    String line = null;

    try {
        input = new RandomAccessFile(named, "r");
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        return;
    } finally {
        if (input != null) {
            input.close();
        }
    }
}
</PRE></BLOCKQUOTE>
    <P><FONT color=green>Answer:</FONT> The code to catch exceptions is shown in 
    red: 
    <BLOCKQUOTE><PRE>public static void cat(File named) {
    RandomAccessFile input = null;
    String line = null;

    try {
        input = new RandomAccessFile(named, "r");
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }
        return;
<FONT color=red>    } catch(FileNotFoundException fnf) {
        System.err.println("File: " + named + " not found.");
    } catch(Exception e) {
        System.err.println(e.toString());</FONT>
    } finally {
        if (input != null) {
<FONT color=red>            try {</FONT>
                input.close();
<FONT color=red>            } catch(IOException io) {
            }</FONT>
        }
    }
}
</PRE></BLOCKQUOTE>
    <P></P></LI></OL>
  <OL></OL></BLOCKQUOTE>
<BLOCKQUOTE></BLOCKQUOTE><IMG height=8 alt="" 
src="Answers to Questions and Exercises Exceptions.files/blueline.gif" width=550 
align=bottom NATURALSIZEFLAG="3"> <BR>
<TABLE width=550 summary=layout>
  <TBODY>
  <TR>
    <TD vAlign=center align=left><A 
      href="http://java.sun.com/docs/books/tutorial/essential/exceptions/QandE/questions.html" 
      target=_top><IMG height=26 alt="Previous Page" 
      src="Answers to Questions and Exercises Exceptions.files/PreviousArrow.gif" 
      width=26 align=center border=0></A> <A 
      href="http://java.sun.com/docs/books/tutorial/essential/TOC.html#exceptions" 
      target=_top><IMG height=26 alt="Lesson Contents" 
      src="Answers to Questions and Exercises Exceptions.files/TOCIcon.gif" 
      width=26 align=center border=0></A> </TD>
    <TD vAlign=center align=middle><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/index.html" 
      target=_top>Start of Tutorial</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/essential/index.html" 
      target=_top>Start of Trail</A> &gt; <A 
      href="http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html" 
      target=_top>Start of Lesson</A> </FONT></TD>
    <TD vAlign=center align=right><FONT size=-1><A 
      href="http://java.sun.com/docs/books/tutorial/search.html" 
      target=_top>Search</A> <BR><A 
      href="http://java.sun.com/docs/books/tutorial/forms/sendusmail.html">Feedback 
      Form</A> </FONT></TD></TR></TBODY></TABLE>
<P><FONT size=-1><A 
href="http://java.sun.com/docs/books/tutorial/information/copyright.html">Copyright</A> 
1995-2003 Sun Microsystems, Inc. All rights reserved. </FONT></P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -