+ 1

I didn't understand Exactly exception...anyone explain please???😅😅

exception

26th Jun 2017, 4:49 PM
Harsh
Harsh - avatar
2 ответов
+ 1
Exceptions are "thrown" if your program has an error and results in crashing your application. And because a crash makes up for bad user experience, you need to handle these in your program everywhere they might be thrown with a try-catch block. But don't try to handle every single error that might come up with exception handling as that is considered "bad". For example: Person p = new Person(); String aGenericFirstName = p.getFirstName(); We want to access the first name of p, but what if the member firstName is null? If it is null our program would throw a NullPointerException and the application crashes. To solve that we could put the second line in a try-catch block and handle this specific exception so that our application won't crash. Something like an console output which states "No first name was set, pls fix" could be enough in the catch block to notify you an error occured but doesn't prevent the application from running. As a better solution you could do a null-check with an if statement (if p.getFirstName() != null) for better performance. Generally said, use the exception handling if you can't prevent the error in advance. I hope I could help you at least a little.
26th Jun 2017, 6:17 PM
Kagemi