---
title: "How can you handle a \"null pointer\" exception to prevent crashes in your C program?"  
description: "How can you handle a \"null pointer\" exception to prevent crashes in your C program?"  
author: "Steilla Mitchel"  
published: 2023-08-09  
updated: 2023-08-16  
canonical: https://www.mindstick.com/forum/159520/how-can-you-handle-a-null-pointer-exception-to-prevent-crashes-in-your-c-program  
category: "C Language"  
tags: ["exception", "error", "c language"]  
reading_time: 3 minutes  

---

# How can you handle a "null pointer" exception to prevent crashes in your C program?

How can you [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) a "[null pointer](https://www.mindstick.com/forum/159532/explain-the-concept-of-null-pointer-dereference-and-how-to-prevent-it-in-c-plus-plus)" [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) to prevent crashes in your C [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program)?

## Replies

### Reply by Aryan Kumar

A [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp) [pointer](https://www.mindstick.com/articles/11/pointers) exception is an error that occurs when a program attempts to dereference a pointer that points to null. This can happen for a variety of reasons, such as:

- The pointer is not initialized.
- The pointer is assigned the value of NULL.
- The pointer is pointing to memory that has been freed.

When a null pointer exception occurs, the program will typically crash. There are a few ways to handle a null pointer exception to prevent crashes in your C program:

- **Use a try-catch block.** The try-catch block is a way to handle errors in C. The try block contains the code that might cause an error, and the catch block handles the error.

Here is an example of how to use a try-catch block to handle a null pointer exception:

C

```plaintext
void foo() {
  int *ptr = NULL; // This is a null pointer

  try {
    // Code that might cause an error
    *ptr = 10; // This will cause a null pointer exception
  } catch (const std::exception& e) {
    // Handle the error
    std::cout << "Null pointer exception!" << std::endl;
  }
}
```

- **Use a smart pointer.** A smart pointer is a type of pointer that automatically checks for null pointers and prevents them from being dereferenced. There are a number of smart pointer libraries available for C.

Here is an example of how to use a smart pointer to handle a null pointer exception:

C

```plaintext
#include <memory>

void foo() {
  std::unique_ptr<int> ptr(new int(10)); // This is a smart pointer

  // Code that might cause an error
  *ptr = 20; // This will not cause a null pointer exception
}
```

- **Exit the program gracefully.** If you cannot handle a null pointer exception gracefully, you can simply exit the program. This will prevent the program from crashing, but it will also lose any unsaved data.

Here are some additional things to keep in mind when handling null pointer exceptions:

- Be careful about using functions that might return null pointers. For example, the `malloc()` function can return a null pointer if there is not enough memory available.
- Use a debugger to find out why a null pointer exception occurred. This can help you to avoid the error in the future.


---

Original Source: https://www.mindstick.com/forum/159520/how-can-you-handle-a-null-pointer-exception-to-prevent-crashes-in-your-c-program

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
