java - Why does my program not accept a custom exception? -


i trying make cat clone , i'm requiring receive input when presented -.

the main(); here:

import java.io.*; import java.util.*;  class cat {   public static void main(string[] args) {     (int = 0; < args.length; i++) {        try{        fileprint(args[i]);          }   catch(dashexception letstrythis){            catdash();         }   catch(filenotfoundexception wrong) {            system.err.println(string.format("%s: file not found.", args[i]));         }   catch (ioexception nowords) {            system.err.println(string.format("%s: file can't read.", args[i]));       }     }   } } 

fileprint() prints file line line , catdash()receives , prints stdin. nothing special.

what i'm trying have custom exception catches - , calls catdash()(first catch block above). however, no matter what, try/catch block throws filenotfound wrong exception (second catch block above). question is, how catch specific cause , throw first before second block does?

my dashexception defined own file:

import java.lang.throwable;  public class dashexception extends filenotfoundexception{   public dashexception(throwable cause){     super("-")   } } 

you not throw custom exception anywhere. need throw in own code in try block. e.g.:

if (args[i].equals("-")) {      throw new dashexception(); } 

and can remove th constuctor, there no root cause exception.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -