diff --git a/py/malloc.c b/py/malloc.c
index 27eaac108826401983855268950a7ed138a551d6..4cf1b71db052c1bdd515b5a86b6e6c398805b0b3 100644
--- a/py/malloc.c
+++ b/py/malloc.c
@@ -41,8 +41,7 @@ void *m_malloc(int num_bytes) {
     }
     void *ptr = malloc(num_bytes);
     if (ptr == NULL) {
-        printf("could not allocate memory, allocating %d bytes\n", num_bytes);
-        return NULL;
+        return m_malloc_fail(num_bytes);
     }
 #if MICROPY_MEM_STATS
     total_bytes_allocated += num_bytes;
@@ -68,8 +67,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
     }
     void *new_ptr = realloc(ptr, new_num_bytes);
     if (new_ptr == NULL) {
-        printf("could not allocate memory, reallocating %d bytes\n", new_num_bytes);
-        return NULL;
+        return m_malloc_fail(new_num_bytes);
     }
 #if MICROPY_MEM_STATS
     // At first thought, "Total bytes allocated" should only grow,
diff --git a/py/misc.h b/py/misc.h
index 35081f18e90f3becfde2ad3a5ba6058c42ffecb5..662513eb1acca9bc251db73eecb97fb28e8871ac 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -36,6 +36,7 @@ void *m_malloc(int num_bytes);
 void *m_malloc0(int num_bytes);
 void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
 void m_free(void *ptr, int num_bytes);
+void *m_malloc_fail(int num_bytes);
 
 int m_get_total_bytes_allocated(void);
 int m_get_current_bytes_allocated(void);
diff --git a/py/obj.h b/py/obj.h
index f5ce873d6bf5975ad48d2b411593d7fed2177d62..fe3c14d11624415e171cde9c1572c102af608100 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -306,6 +306,7 @@ extern const struct _mp_obj_bool_t mp_const_false_obj;
 extern const struct _mp_obj_bool_t mp_const_true_obj;
 extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
 extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
+extern const struct _mp_obj_exception_t mp_const_MemoryError_obj;
 extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
 
 // General API for objects
diff --git a/py/objexcept.c b/py/objexcept.c
index 0efc21d3606924b38c668f9010dcc894dd46a916..11651025f06fd30a26f8f63f236c5f42035cc67b 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -17,6 +17,9 @@ typedef struct _mp_obj_exception_t {
     mp_obj_tuple_t args;
 } mp_obj_exception_t;
 
+// Instance of MemoryError exception - needed by mp_malloc_fail
+const mp_obj_exception_t mp_const_MemoryError_obj = {{&mp_type_MemoryError}, MP_OBJ_NULL, {{&mp_type_tuple}, 0}};
+
 // Instance of GeneratorExit exception - needed by generator.close()
 // This would belong to objgenerator.c, but to keep mp_obj_exception_t
 // definition module-private so far, have it here.
diff --git a/py/runtime.c b/py/runtime.c
index d9e2298c4cb35f6dfcdab56eef35e2e9f8e9282a..a5afe0e7508d8dc1e575bb5c31406dd1f3235140 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -1034,6 +1034,11 @@ void mp_globals_set(mp_map_t *m) {
     map_globals = m;
 }
 
+void *m_malloc_fail(int num_bytes) {
+    DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
+    nlr_jump((mp_obj_t)&mp_const_MemoryError_obj);
+}
+
 // these must correspond to the respective enum
 void *const mp_fun_table[MP_F_NUMBER_OF] = {
     mp_load_const_dec,
diff --git a/py/vm.c b/py/vm.c
index 69f350f9fddc4e5b75b635312dc5e4c55b8104bc..6186bbcefbf10f00e8c22eef205ff7d78d96fc86 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -849,8 +849,8 @@ yield:
             // set file and line number that the exception occurred at
             // TODO: don't set traceback for exceptions re-raised by END_FINALLY.
             // But consider how to handle nested exceptions.
-            // TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj)
-            if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj) {
+            // TODO need a better way of not adding traceback to constant objects (right now, just GeneratorExit_obj and MemoryError_obj)
+            if (mp_obj_is_exception_instance(nlr.ret_val) && nlr.ret_val != &mp_const_GeneratorExit_obj && nlr.ret_val != &mp_const_MemoryError_obj) {
                 machine_uint_t code_info_size = code_info[0] | (code_info[1] << 8) | (code_info[2] << 16) | (code_info[3] << 24);
                 qstr source_file = code_info[4] | (code_info[5] << 8) | (code_info[6] << 16) | (code_info[7] << 24);
                 qstr block_name = code_info[8] | (code_info[9] << 8) | (code_info[10] << 16) | (code_info[11] << 24);
diff --git a/tests/basics/memoryerror.py b/tests/basics/memoryerror.py
new file mode 100644
index 0000000000000000000000000000000000000000..b4be420c316237e3f452069fa02cdabb42d83b4b
--- /dev/null
+++ b/tests/basics/memoryerror.py
@@ -0,0 +1,6 @@
+l = list(range(10000))
+try:
+    100000000 * l
+except MemoryError:
+    print('MemoryError')
+print(len(l), l[0], l[-1])