Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: exception-throwing class?

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);
    }
}
like image 521
hhh Avatar asked Apr 13 '10 18:04

hhh


1 Answers

The throws keyword cannot be applied at class level, only at the method level.

like image 169
fastcodejava Avatar answered Oct 04 '22 01:10

fastcodejava