We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Remember, you need to either use a try catch block or add "throws (Exception)" to a method name in order to throw a checked exception. In this case, there is no method name for the static initializer block, so you must use a try catch!
See code below:
MyCode{
static Scanner input = new Scanner(System.in);
static boolean flag = true;
static int B = input.nextInt();
static int H = input.nextInt();
static{
try{
if(B <= 0 || H <= 0){
flag = false;
throw new Exception("Breadth and height must be positive");
}
}catch(Exception e){
System.out.println(e);
}
}
Java Static Initializer Block
You are viewing a single comment's thread. Return to all comments →
Remember, you need to either use a try catch block or add "throws (Exception)" to a method name in order to throw a checked exception. In this case, there is no method name for the static initializer block, so you must use a try catch!
See code below:
MyCode{
}