Tricky Tricks ›› Programming Tricks ›› Java Tricks ›› If You Don't Want To Deal With Exception Handling

If You Don't Want To Deal With Exception Handling

Java Programming Tricks

If you don't want to deal with exception handling:

 When writing a quick and dirty program, it is a hassle to have to pay attention 
 to all the exceptions that are thrown. 
 
 public void writeStack(DataOutput out) 
 
 { for (int i = 0; i < 100; i-.+) 
 
   { String s = (String) stk.pop(); // compiler complains: EmptyStackException 
 
      out.writeChars(n); // compiler complains: IOException 
 
   }
 
 }
 
 Tip 
 
 Add throws Exception after every method, even main! 
 
 public void writeStack(DataOutput out) throws Exception 
 
 { for (int i = 0; i < 100; itt) 
 
    { String s = (String) stk.pop(); // compiler doesn't complain 
 
      out.writeChars(n); // compiler doesn't complain 
 
    } 
 
 }
 
 public static void main () throws Exception 
 
 {  . . .
 
    outFile = . . . 
 
    writeStack(outFile); // compiler doesn't complain 
 
    .  .  .
 
 } 
 
 Note 
 
 This is definitely a tactical move for prototyping only. Once you finished your 
 code exploration, you should put the exception handling code at the proper place, 
 and make the exception specifications of all methods precise.
 

Partners