抛出异常并通过子类实现

时间:2012-02-24 02:01:54

标签: java exception throw

我正在尝试创建一个名为HashException的java.lang.RuntimeException的子类,当我的NumStream类遇到哈希字符(#)时,我必须修改NumStream来抛出它。不太清楚如何实现这一点,我有一些东西,它确实抛出了异常,但我想知道是否有更清晰的方法来实现这一点,或者即使我正确地做到了。

import java.io.*;
import java.lang.*;

class HashException extends java.lang.RuntimeException
{
    public void write(int c)
    {
        if(c == 35) throw new RuntimeException("This is a Hash Tag!");
    }
}

public class NumStream extends OutputStream
{
    public void write(int c) throws IOException
    {  
        StringBuffer sb = new StringBuffer();

        HashException h = new HashException();
        h.write(c);

        switch(c)
        {
            case ' ': sb.append(" ");
                break;
            case '1': sb.append("One");
                break;
            case '2': sb.append("Two");
                break;
            case '3': sb.append("Three");
                break;
            case '4': sb.append("Four");
                break;                
            case '5': sb.append("Five");
                break; 
            case '6': sb.append("Six");
                break;
            case '7': sb.append("Seven");
                break;
            case '8': sb.append("Eight");
                break;     
            case '9': sb.append("Nine");
                break; 
            case '0': sb.append("Zero");
                break;
            default:  sb.append(Integer.toString(c));
                break;
        }
        System.out.print(sb);
    }
    public static void main(String[] args) 
    {
        NumStream ns = new NumStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(ns));
        pw.println("123456789 and ! and # ");
        pw.flush();
    }
}

这是我的输出:

run:
Exception in thread "main" java.lang.RuntimeException: This is a Hash Tag!
    at HashException.write(NumStream.java:8)
    at NumStream.write(NumStream.java:19)
OneTwoThreeFourFiveSixSevenEightNine and ! and  at     java.io.OutputStream.write(OutputStream.java:116)
    at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
    at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
    at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
    at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
    at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
    at java.io.PrintWriter.flush(PrintWriter.java:320)
    at NumStream.main(NumStream.java:54)
Java Result: 1

1 个答案:

答案 0 :(得分:2)

检查应该在你的numstream类中。如果遇到哈希,则抛出哈希异常。像这样:

 switch(c) {
            case '#':
                throw new HashException();
            case ' ': sb.append(" ");
                break;
            case '1': sb.append("One");
                break;
            case '2': sb.append("Two");
                break;
            case '3': sb.append("Three");
                break;
            case '4': sb.append("Four");
                break;                
            case '5': sb.append("Five");
                break; 
            case '6': sb.append("Six");
                break;
            case '7': sb.append("Seven");
                break;
            case '8': sb.append("Eight");
                break;     
            case '9': sb.append("Nine");
                break; 
            case '0': sb.append("Zero");
                break;
            default:  sb.append(Integer.toString(c));
                break;
        }

您也可以在message类中声明例外HashException

class HashException extends Exception
{
    public HashException() {
       super("A hash was encountered!");
    }
}

请注意,HashException可能应该扩展Exception,而不是RuntimeException