Shreyas arya

Generation

generate functionFri, 10 Feb 2023

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

public static void printString(String str) { if (str instanceof String) { System.out.println(str); } else { for (int i = 0; i < 3; i++) { System.out.println("Try again"); try { throw new Exception("Invalid String"); } catch (Exception e) { System.out.println(e.getMessage()); } } } }

Questions about programming?Chat with your personal AI assistant