I am trying to get a specific exception
Input H=-1; B=2;
Expected Output
java.lang.Exception: Breadth and height must be positive
Current Output
-2
public class Solution {
static int H,B;
static boolean flag = true;
static //static initializer block
{
Scanner sc = new Scanner(System.in);
H=sc.nextInt();
B=sc.nextInt();
}
public static void main(String[] args){
if(flag){
int area=B*H;
System.out.print(area);
}
}
}
How do I get that specific exception?
You can throw your own exception like this
if(H<0 || B<0){
throw new Exception("Breadth and height must be positive");
}
However, it would be a better practice to use something like a IllegalArgumentException
for this.
if(H<0 || B<0){
throw new IllegalArgumentException("Breadth and height must be positive");
}