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);
}
}
The throws
keyword cannot be applied at class level, only at the method level.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With