Shreyas arya

Generation

generate functionFri, 10 Feb 2023

Create a method print a string If type is not string then throw exception and retrun exception and retry 3 times

public class JavaClass { public static void main(String[] args){ } public boolean isString(Object obj){ return obj instanceof String; } public static void printString(Object obj) throws Exception { if (isString(obj)){ System.out.println((String) obj); } else { throw new Exception("Not a string"); } } }

Questions about programming?Chat with your personal AI assistant