Untitled Note

By: Anonymous2/9/20222 views Public Note
package com.capgemini.lesson9; // method should accept name parameter // check if it is null, //throw NullPointerExcpetion with message "Name can not be null"C // check if name have more than 3 letters, if name has less than 3 letters // WrongNameLengthException with message "Name should have more than 3 letters" class WrongNameLengthException extends Exception{ public WrongNameLengthException() {} public WrongNameLengthException(String s) { super(s); } } class BlankNameException extends Exception{ public BlankNameException() {} } class Demo { public static String printNameInUpperCase(String name){ String result=""; try { if(name==null) { throw new NullPointerException(); } if(name=="") { throw new BlankNameException(); } if(name.length()<4) { throw new WrongNameLengthException(); } result= name.toUpperCase(); }catch(NullPointerException ne) { return "Name can not be null"; }catch(WrongNameLengthException e) { return "Name should have more than 3 letters"; }catch(BlankNameException b) { return "Name should not be blank"; } return result; } } public class Source { public static void main(String[] args) { String input=""; String output=Demo.printNameInUpperCase(input); System.out.println(output); } }

Want to create your own notes?

Join thousands of users writing securely on ProNotepad.