This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Impossible to catch exception

5 replies
jens.tinz
Joined: 2011-07-19,
User offline. Last seen 42 years 45 weeks ago.
P {margin-top:0;margin-bottom:0;} Hello!

Using scala 2.9.0.1 and JDK1.6.0_23, the code posted below causes a java.io.NotSerializableException.
However, the line following the one causing the exception is executed and the catch-block is ignored.

Am I overlooking something or is there a serious bug in scala or Java?


Thank you,
Jens


// source --------------------------------------------------------------------------

package test

import java.awt.Toolkit
import java.awt.datatransfer.{Transferable, DataFlavor}

class Test

object Test {
  val flavor = new DataFlavor(classOf[Array[Test]], "Test Array")
}

class ClipboardData(data: Test) extends Transferable {
  override def getTransferDataFlavors : Array[DataFlavor] =
    Array(Test.flavor)

  override def isDataFlavorSupported(flavor: DataFlavor) : Boolean =
    flavor == Test.flavor

  override def getTransferData(flavor: DataFlavor) : AnyRef =
    data
}

object Main {
  def main(args: Array[String]) {
    val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
    val data = new ClipboardData(new Test)
    try {
      clipboard.setContents(data, null)
      println("exception ignored")
    } catch {
      case _ => println("exception caught")
    }
  }
}

// output --------------------------------------------------------------------------

java.io.NotSerializableException: test.Test
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
    at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1217)
    at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163)
    at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73)
    at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93)
    at test.Main$.main(Main.scala:28)
    at test.Main.main(Main.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
exception ignored
jens.tinz
Joined: 2011-07-19,
User offline. Last seen 42 years 45 weeks ago.
RE: Impossible to catch exception
P {margin-top:0;margin-bottom:0;} Hello!

I've ported the code to Java and it shows the same behavior.
I'll post this to the Oracle bug database.


Regards,
Jens


// java source --------------------------------------------------------------------------

package test;

import java.awt.Toolkit;
import java.awt.datatransfer.*;

class Test {
  public static DataFlavor flavor = new DataFlavor(Test[].class, "Test Array");
}

class ClipboardData implements Transferable {
  Test data = null;

  ClipboardData(Test pdata) {
      data = pdata;
  }

  public DataFlavor[] getTransferDataFlavors() {
      return new DataFlavor[] { Test.flavor };
  }

  public boolean isDataFlavorSupported(DataFlavor flavor) {
    return flavor == Test.flavor;
  }

  public Object getTransferData(DataFlavor flavor) {
    return data;
  }
}

public class Main {
    public static void main(String[] args) {
      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      ClipboardData data = new ClipboardData(new Test());
      try {
        clipboard.setContents(data, null);
        System.out.println("exception ignored");
      } catch(Exception e) {
        System.out.println("exception caught");
      }
    }
}

// output from java source --------------------------------------------------------------------------

java.io.NotSerializableException: test.Test
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1399)
    at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:189)
    at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:87)
    at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:110)
    at test.Main.main(Main.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
exception ignored

From: scala-language [at] googlegroups [dot] com [scala-language [at] googlegroups [dot] com] on behalf of Jens [dot] Tinz [at] dlr [dot] de [Jens [dot] Tinz [at] dlr [dot] de]
Sent: Tuesday, July 19, 2011 4:26 PM
To: scala-language [at] googlegroups [dot] com
Subject: [scala-language] Impossible to catch exception

Hello!

Using scala 2.9.0.1 and JDK1.6.0_23, the code posted below causes a java.io.NotSerializableException.
However, the line following the one causing the exception is executed and the catch-block is ignored.

Am I overlooking something or is there a serious bug in scala or Java?


Thank you,
Jens


// source --------------------------------------------------------------------------

package test

import java.awt.Toolkit
import java.awt.datatransfer.{Transferable, DataFlavor}

class Test

object Test {
  val flavor = new DataFlavor(classOf[Array[Test]], "Test Array")
}

class ClipboardData(data: Test) extends Transferable {
  override def getTransferDataFlavors : Array[DataFlavor] =
    Array(Test.flavor)

  override def isDataFlavorSupported(flavor: DataFlavor) : Boolean =
    flavor == Test.flavor

  override def getTransferData(flavor: DataFlavor) : AnyRef =
    data
}

object Main {
  def main(args: Array[String]) {
    val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
    val data = new ClipboardData(new Test)
    try {
      clipboard.setContents(data, null)
      println("exception ignored")
    } catch {
      case _ => println("exception caught")
    }
  }
}

// output --------------------------------------------------------------------------

java.io.NotSerializableException: test.Test
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
    at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1217)
    at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163)
    at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73)
    at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93)
    at test.Main$.main(Main.scala:28)
    at test.Main.main(Main.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
exception ignored
Bob Jamison
Joined: 2009-07-23,
User offline. Last seen 42 years 45 weeks ago.
Re: RE: Impossible to catch exception

On 7/20/2011 2:57 AM, Jens [dot] Tinz [at] dlr [dot] de wrote:
> Hello!
>
> I've ported the code to Java and it shows the same behavior.
> I'll post this to the Oracle bug database.
>
>
> Regards,
> Jens
>
>
> // java source --------------------------------------------------------------------------
>
> package test;
>
> import java.awt.Toolkit;
> import java.awt.datatransfer.*;
>
> class Test {
> public static DataFlavor flavor = new DataFlavor(Test[].class, "Test Array");
> }
>
> class ClipboardData implements Transferable {
> Test data = null;
>
> ClipboardData(Test pdata) {
> data = pdata;
> }
>
> public DataFlavor[] getTransferDataFlavors() {
> return new DataFlavor[] { Test.flavor };
> }
>
> public boolean isDataFlavorSupported(DataFlavor flavor) {
> return flavor == Test.flavor;
> }
>
> public Object getTransferData(DataFlavor flavor) {
> return data;
> }
> }
>
> public class Main {
> public static void main(String[] args) {
> Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
> ClipboardData data = new ClipboardData(new Test());
> try {
> clipboard.setContents(data, null);
> System.out.println("exception ignored");
> } catch(Exception e) {
> System.out.println("exception caught");
> }
> }
> }
>
> // output from java source --------------------------------------------------------------------------
>
> java.io.NotSerializableException: test.Test
> at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
> at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
> at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1399)
> at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:189)
> at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:87)
> at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:110)
> at test.Main.main(Main.java:35)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:601)
> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
> exception ignored
>
> ________________________________
> From: scala-language [at] googlegroups [dot] com [scala-language [at] googlegroups [dot] com] on behalf of Jens [dot] Tinz [at] dlr [dot] de [Jens [dot] Tinz [at] dlr [dot] de]
> Sent: Tuesday, July 19, 2011 4:26 PM
> To: scala-language [at] googlegroups [dot] com
> Subject: [scala-language] Impossible to catch exception
>
> Hello!
>
> Using scala 2.9.0.1 and JDK1.6.0_23, the code posted below causes a java.io.NotSerializableException.
> However, the line following the one causing the exception is executed and the catch-block is ignored.
>
> Am I overlooking something or is there a serious bug in scala or Java?
>
>
> Thank you,
> Jens
>
>
> // source --------------------------------------------------------------------------
>
> package test
>
> import java.awt.Toolkit
> import java.awt.datatransfer.{Transferable, DataFlavor}
>
> class Test
>
> object Test {
> val flavor = new DataFlavor(classOf[Array[Test]], "Test Array")
> }
>
> class ClipboardData(data: Test) extends Transferable {
> override def getTransferDataFlavors : Array[DataFlavor] =
> Array(Test.flavor)
>
> override def isDataFlavorSupported(flavor: DataFlavor) : Boolean =
> flavor == Test.flavor
>
> override def getTransferData(flavor: DataFlavor) : AnyRef =
> data
> }
>
> object Main {
> def main(args: Array[String]) {
> val clipboard = Toolkit.getDefaultToolkit.getSystemClipboard
> val data = new ClipboardData(new Test)
> try {
> clipboard.setContents(data, null)
> println("exception ignored")
> } catch {
> case _ => println("exception caught")
> }
> }
> }
>
> // output --------------------------------------------------------------------------
>
> java.io.NotSerializableException: test.Test
> at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
> at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
> at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1217)
> at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163)
> at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73)
> at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93)
> at test.Main$.main(Main.scala:28)
> at test.Main.main(Main.scala)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:597)
> at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
> exception ignored
>

Maybe they have mislabeled a runtime Error as an Exception? Last time I
looked, the way to handle an anticipated Error was to spawn a Thread and
attempt the code. The Thread dies on failure, but the rest of your
program survives.

Just a guess.

Bob (ishmal)

nilskp
Joined: 2009-01-30,
User offline. Last seen 1 year 27 weeks ago.
Re: RE: Impossible to catch exception
On Wed, Jul 20, 2011 at 9:57 AM, <Jens [dot] Tinz [at] dlr [dot] de> wrote:
Hello!

I've ported the code to Java and it shows the same behavior.

They are not identical. In Scala you catch Throwable and in Java you catch Exception.
Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Impossible to catch exception

On 20/07/2011 09:57, Jens [dot] Tinz [at] dlr [dot] de wrote:
> I've ported the code to Java and it shows the same behavior.
> I'll post this to the Oracle bug database.

Puzzling, indeed. NotSerializableException is a good old Exception, but even catching a
Throwable doesn't get it.

Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Impossible to catch exception

On 20/07/2011 11:19, Bob Jamison wrote:
> Maybe they have mislabeled a runtime Error as an Exception? Last time I looked, the way to
> handle an anticipated Error was to spawn a Thread and attempt the code. The Thread dies on
> failure, but the rest of your program survives.

Ah, good hint! If we look at Clipboard.setContent's source, we can see the following snippet:

EventQueue.invokeLater(new Runnable() {
public void run() {
oldOwner.lostOwnership(Clipboard.this, oldContents);
}
});

So the exception probably happens in a separate thread, hence no exception caught.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland