What is the point of throwing exceptions? For example, I came across this:
static List<Integer> list(int [] a) { if (a == null) throw new NullPointerException(); //...
But when you don't drop nullpointer, will you also get nullpointer? I see it regularly, and I would like to know if it is good to study?
Better to fail quickly. For example, a function can do a bunch of stuff before it even refers to the variable "a" in your example, which leads to a lot of unnecessary processing. It would be best to crash right away if you know that "a" is null from the very beginning. You can also add a special error message to the exception.
THROW , .
, . , , .
, , .
, . , , -, , . - . , ...
FlowControl → GenericMethod ( ) → PrivateMethods ( , ).
: Exception vs ?
, - , . , "ToList", :
static List<Integer> list(int[] a) { List<int> ret = new List<int>(); foreach (int i in a) ret.add(i); return ret; }
, , ( , , , # , foreach). , throw, .
foreach
throw
, -, . ( - null, null), ( , ) .
Basically, when you arbitrarily throw exceptions, each person using your library, for example, can save the correct thread.
Source: https://habr.com/ru/post/1607330/More articles:JavaScript - nested array filter - javascriptWhat will be the behavior of subscription and notifications in the Orion load balancing scenario? - publish-subscribeRpyc connection closed by peer - pythonHow to hide Telegram BOT teams when they are part of a group? - telegramCasting CustomError for ErrorType for NSError loses userInfo - swiftPHP AWS HTTP Error: Cannot be opened using r + fopen mode - phpWhen to catch an Exception vs. When to throw an Exception? - javaIBOutlet and IBAction in Swift - swiftHow would pythonista code the equivalent of the increment ++ operator in Python 3? - pythonHow to implement whatsapp messenger using MQTT? - mqttAll Articles