# Catching image failure is not working (try/catch)

**URL:** <https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890>\
**Category:** Beginners\
**Created:** [February 10, 2023, 3:53pm UTC](https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890 "2023-02-10T15:53:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [February 10, 2023, 3:53pm UTC](https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890/1 "2023-02-10T15:53:16Z")

</div>

This doesnt work!

```auto
void setup() {
   try {
      image = loadImage("image.png");
   } catch (Exception e) {
      image = loadImage("error.png");
   }
}

```

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [February 10, 2023, 4:48pm UTC](https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890/2 "2023-02-10T16:48:49Z")

</div>

Hi @Aryszin,

I would recommend, before you’re posting things like this, to always consider to read the manuals, resp. references about. It clearly descibes on how to do error checking in several ways…

> **[loadImage() / Reference](https://processing.org/reference/loadImage_.html)**
>
> Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the …

Cheers  
— mnse

PS: to give a sneak preview… loadImage doesn’t throw exceptions… 😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 10, 2023, 11:20pm UTC](https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890/3 "2023-02-10T23:20:15Z")

</div>

void setup() {  
image = loadImage(“image.png”);  
if (image==null) {  
image = loadImage(“error.png”);  
}  
}

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 10, 2023, 11:42pm UTC](https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890/4 "2023-02-10T23:42:59Z")

</div>

> [@mnse](#):
>
> … **loadImage()** doesn’t throw exceptions… 😉

According to **[loadImage()](https://processing.org/reference/loadImage_.html)**'s reference, besides `null`, negative dimensions might also happen to indicate failure.

So if we need to be absolutely sure, we gotta check them as well, not just `null`:

```auto
PImage image;

void setup() {
  size(600, 600);
  noLoop();

  image = loadImage("image.png");

  if (!isValidImage(image))
    image = loadImage("https://CDN-Icons-png.FlatIcon.com/512/2731/2731804.png");

  println(image.width, image.height);
}

void draw() {
  background(0);
  set(width - image.width >> 1, height - image.height >> 1, image);
}

static final boolean isValidImage(final PImage img) {
  return img != null && img.width > 0 && img.height > 0;
}

```

---

<div class="post-metadata">

**Author:** ![Aryszin](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Aryszin](https://discourse.processing.org/u/Aryszin)\
**Post date:** [February 11, 2023, 5:37pm UTC](https://discourse.processing.org/t/catching-image-failure-is-not-working-try-catch/40890/5 "2023-02-11T17:37:10Z")

</div>

im sorry i didnt really know that
