Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- MyObject* obj = new MyObject();
- if(obj!=NULL) {
- //do something
- } else {
- //??? most code I see has log-it and move on
- //or it repeats what's in the exception handler
- }
- } catch(std::bad_alloc) {
- //Do something? normally--this code is wrong as it allocates
- //more memory and will likely fail, such as writing to a log file.
- }
- //X is non-negative.
- //Returns an object or throws exception.
- MyObject* create(int x) {
- if(x<0) throw;
- return new MyObject();
- }
- try {
- MyObject* x = create(unknownVar);
- if(x!=null) {
- //is this null check really necessary?
- }
- } catch {
- //do something
- }
- //Internal use only--non-public, not part of public API
- //input must be non-null.
- //returns non-negative value, or -1 if failed
- int ParseType(String input) {
- if(input==null) return -1;
- //do something magic
- return value;
- }
- //Internal use only--non-public, not part of public API
- //input must be non-null.
- //returns non-negative value
- int ParseType(String input) {
- assert(input!=null : "Input must be non-null.");
- //do something magic
- return value;
- }
- template<typename T>
- static inline void nullcheck(T *ptr) {
- #if PLATFORM_TRAITS_NEW_RETURNS_NULL
- if (ptr == NULL) throw std::bad_alloc();
- #endif
- }
- internal void DoThis(Something thing)
- {
- Debug.Assert(thing != null, "Arg [thing] cannot be null.");
- //...
- }
- public void DoThis(Something thing)
- {
- if (thing == null)
- {
- throw new ArgumentException("Arg [thing] cannot be null.");
- }
- //...
- }
- if(obj==NULL)
- throw std::bad_alloc();
- public MyObject MyMethod(object foo)
- {
- if (foo == null)
- {
- throw new ArgumentNullException("foo");
- }
- // do whatever if foo was non-null
- }
Add Comment
Please, Sign In to add comment