Added code to shutdown device if the device is already in booted state while erasing content and settings

You probably want 'if' not 'else if'. You should throw exception even of Line 243 fails.