Java: exception-throwing class?
Posted
by HH
on Stack Overflow
See other posts from Stack Overflow
or by HH
Published on 2010-04-13T18:43:20Z
Indexed on
2010/04/13
18:53 UTC
Read the original article
Hit count: 585
I have classes DirReader and Search. The search uses DirReader. I want the search to know when DirReader throws exception. So how can I have class throwing exception?
Currently, I use initCorrect -dummy var. Exception-style method may be more appropriate.
Simplified Example Error
$ javac ExceptionStatic.java
ExceptionStatic.java:4: '{' expected
public class ExceptionStatic throws Exception{
^
1 error
Code
import java.util.*;
import java.io.*;
// THIS PART NEEDS TO BE FIXED:
public class ExceptionStatic throws Exception{
private static boolean initCorrect = false;
public static String hello;
static{
try{
hello = "hallo";
//some other conditionals in real code
if( true) throw new Exception();
initCorrect=true;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
if(initCorrect)
System.out.println(hello);
}
}
© Stack Overflow or respective owner