# Error handling for the clipboard

**URL:** https://discourse.processing.org/t/error-handling-for-the-clipboard/16654
**Category:** Coding Questions
**Created:** [December 27, 2019, 11:54am UTC](https://discourse.processing.org/t/error-handling-for-the-clipboard/16654 "2019-12-27T11:54:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [December 27, 2019, 11:54am UTC](https://discourse.processing.org/t/error-handling-for-the-clipboard/16654/1 "2019-12-27T11:54:14Z")

</div>

I am using the following code to capture text from the clipboard, however if I use print screen the clipboard will be filled with image data and return an error.

```auto
Object getFromClipboard (DataFlavor flavor) {

  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); 
  Transferable contents = clipboard.getContents(null);
  Object object = null;

  if (contents != null && contents.isDataFlavorSupported(flavor))
  {
    try
    {
      object = contents.getTransferData(flavor);
      println("Clipboard.getFromClipboard() >> Object transferred from clipboard.");
    }

    catch (UnsupportedFlavorException e1) // Unlikely but we must catch it
    {
      println("Clipboard.getFromClipboard() >> Unsupported flavor: " + e1);
      //~ e1.printStackTrace();
    }

    catch (java.io.IOException e2)
    {
      println("Clipboard.getFromClipboard() >> Unavailable data: " + e2);
      //~ e2.printStackTrace();
    }
  }

  return object;
}

String getTextFromClipboard () {
  String text = (String) getFromClipboard(DataFlavor.stringFlavor);
 
  if (text==null) 
    return "";
 
  return text;
}

```

How do I implement error handling to deal with this eventuality?

it seems the line in question is this one

```auto
Transferable contents = clipboard.getContents(null);

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [December 27, 2019, 4:52pm UTC](https://discourse.processing.org/t/error-handling-for-the-clipboard/16654/2 "2019-12-27T16:52:29Z")

</div>

It would be better to implement this code in a class. G4P has a class, [GClip](https://sourceforge.net/p/g4p/code/ci/master/tree/G4P/src/g4p_controls/GClip.java), to handle copying and pasting of text, you might try incorporating this class in your sketch.

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [December 28, 2019, 12:13am UTC](https://discourse.processing.org/t/error-handling-for-the-clipboard/16654/3 "2019-12-28T00:13:32Z")

</div>

> [@paulgoux](#):
>
> however if I use print screen the clipboard will be filled with image data and return an error

What OS are you using for this? I am not able to repro using P3.5.3 and Win10x64. I have

```auto
void mouseReleased() {
  print(getTextFromClipboard());
}

```

and if I have an image capture using the prtScr key, the code does not execute as no error is thrown. If you get an error, you are saying that the line

`Transferable contents = clipboard.getContents(null);`

gets highlighted by the PDE?

Kf

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [December 28, 2019, 2:06am UTC](https://discourse.processing.org/t/error-handling-for-the-clipboard/16654/4 "2019-12-28T02:06:52Z")

</div>

thats it, I’m using windows 10, with the latest version of processing

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c4b4ee605b16807a9900cda5a6733fe780b87abe.png)
