1

in an application I made that uses AWT standard classes, a call to Color constructor throws the following Exception message:

run:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Color parameter outside of expected range: Red
    at java.awt.Color.testColorValueRange(Color.java:310)
    at java.awt.Color.<init>(Color.java:395)
    at java.awt.Color.<init>(Color.java:369)
    at mandelbrotset.DrawingArea.translateColor(DrawingArea.java:106)
    at mandelbrotset.DrawingArea.drawMandelbrotSet(DrawingArea.java:75)
    at mandelbrotset.DrawingArea.paintComponent(DrawingArea.java:45)
    at javax.swing.JComponent.paint(JComponent.java:1054)

is there any way to change this message (including in this case the value of red component)?

I thought of defining a new class NewColor that extends Color and throws the modified exception in the constructor, but the new class constructor must start with a call to super(red, green, blu) and so the standard testColorValueRange(int r, int g, int b, int a) is called and the standard exception is thrown.

Thanks for any help.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
sthor69
  • 638
  • 1
  • 10
  • 25
  • You could always catch the exception. Otherwise you could modify bytecode, which in the core classes, would scare me. – Dave Newton Jan 16 '12 at 15:05
  • You can't make Java use your `NewColor` class in JDK code, you'd only get the new message for instances you're creating yourself. Also, `testColorValueRange` seems to be `private`, so you can't override it anyway. Why not just log what parameters you're passing into the constructor that throws the exception? – millimoose Jan 16 '12 at 15:07
  • Where are you calling `new Color()`? Can't you check the value before creating the object? Or just use a debugger? – Viruzzo Jan 16 '12 at 15:08
  • Why do you want to do something like that? – Bhesh Gurung Jan 16 '12 at 15:10
  • Checkout my Answer here https://stackoverflow.com/questions/56840409/how-to-change-status-code-in-spring-boot-error-response/66680203#66680203 – Ugonna Ubaka Mar 17 '21 at 20:06

3 Answers3

4

Catch the unwanted exception and throw the desired exception. Something like this:

try
{
  blah; // throws the unwanted exception.
}
catch (UnwantedExceptionType exception)
{
  throw new DesiredException(exception);  // option 1.
  // or 
  throw new DesiredException(new info, exception) // option 2.
}

You can create a new exception class (DesiredException in the example above) that takes the existing exception and "changes" the values to what you want.

It is also possible that (in the code above) UnwantedExceptionType and DesiredException are the same type, you just set the desired values in DesiredException.

DwB
  • 37,124
  • 11
  • 56
  • 82
0

You should be able to catch the exception where you test the value range, and output an error message there with the values for r, g, b & a.

Or you could walk through the code in a debugger and verify the values that way.

Alex
  • 2,405
  • 4
  • 23
  • 36
0

Defining a new class that extends color would be the best way to modify the type of Exception, in my opinion. There is a reason that it restricts the range of red from 0 to 255. If you're trying to override that then you may have to override some more logic later on to correct for that as well.

aoi222
  • 723
  • 1
  • 5
  • 11