Queen Attack - error no exception thrown even though I do this

Good Evening,

I coded this program yesterday and included a throw case but it doesn’t provide the output asked for (which I don’t exactly know)

I have this code:

function canattack(white::Queen, black::Queen)
#   pos valid?
    if black.x > 7 || black.x < 0 || black.y > 7 || black.y < 0
        #throw(MethodError(canattack, (white, black)))
#        throw(InvalidPosition)
        throw(InvalidPosition(black.x, black.y))
    end
    if white.x > 7 || white.x < 0 || white.y > 7 || white.y < 0 
        #throw(MethodError(canattack, (white, black)))
#        throw(InvalidPosition)
        throw(InvalidPosition(white.x, white.y))
    end

the results show:

### Code Run

@test_throws ExercismTestReports.InvalidPosition Queen(8, 4)


### Test Failure

Test Failed at ./runtests.jl:11 Expression: Queen(8, 4) Expected: ExercismTestReports.InvalidPosition No exception thrown

it seems I'm doing something sligthly different
or should the exception be thrown when creating the queen-structs??

thanks for your answers in advance,

Nisang

What’s your full solution code?

Have you defined your InvalidPosition struct to have 2 fields?

struct InvalidPosition <: Exception 
           T
           x::Int8
           y::Int8
end

struct Queen
    x::Int8
    y::Int8
end

function Queen(x::Int8,y::Int8)
    if x > 7 || x < 0 || y > 7 || y < 0
        #throw(MethodError(canattack, (white, black)))
        throw(InvalidPosition(black.x, black.y))
    end
end

function canattack(white::Queen, black::Queen)
    @show white.x, white.y,  black.x, black.y
#   pos valid?
    if black.x > 7 || black.x < 0 || black.y > 7 || black.y < 0
        throw(InvalidPosition(Queen,black.x, black.y))
        throw(InvalidPosition(black.x, black.y))
    end
    if white.x > 7 || white.x < 0 || white.y > 7 || white.y < 0 
        throw(InvalidPosition(white.x, white.y))
    end
#   check horizontal or vertical in-line:
    if white.x == black.x || white.y == black.y
        return true
    else
#   check diagonal in-line:
        x_dif = white.x - black.x
#=   don't have to check if result is on-board as pos validity checked before!
        if white.y + x_dif < max.y && white.y + x_dif == black.y || white.y - x_dif > min.y && white.y - x_dif == black.y=#
        if white.y + x_dif == black.y || white.y - x_dif == black.y
            return true
        end
    end
    return false
end

before it was:

struct InvalidPosition <: Exception 
           x::Int8
           y::Int8
end

and:
        throw(InvalidPosition(black.x, black.y))

function Queen 

has been added afterwards as throw(InvalidPosition(black.x, black.y)) in canattack() didn’t bring the demanded effect

Since you changed the code in between posts, are you getting the same error message from before or something else now?

well the main part (80%) of the code works,
the erorr

No exception thrown

still remains

Let’s double check some assumptions then. In Queen(8, 4), are we sure the two numbers are Int8?

I’m confused
yes, they are…
as in the code above.

!Now I got, what you mean, see message below!

You have a struct with 3 members

And you call it with 2 arguments:

throw(InvalidPosition(white.x, white.y))
julia> typeof(8)
Int64
julia> typeof(4)
Int64

So we have a few more facts to work with.

  1. We’re not passing Int8s.
  2. The Queen constructor expects Int8s.
  3. Julia isn’t raising an error about the mismatch.
  4. A struct is still created without error.

Given 1, 2, and 3, it seems we’re not dispatching to your constructor. Otherwise, x > 7 would be true and we would have thrown your InvalidPosition. So how is the struct being created?

Good evening Andras

okay,
I considered your points …
and changed the code to

struct InvalidPosition <: Exception 
#           T
           x::Int64
           y::Int64
end

struct Queen
    x::Int64
    y::Int64
end

And used
throw(InvalidPosition(black.x, black.y))

And still the error remains:

Test Failed at ./runtests.jl:11
  Expression: Queen(8, 4)
    Expected: ExercismTestReports.InvalidPosition
  No exception thrown

so the questions are:

  • as I do throw an error… - which works fine in the REPL
    why the error is I didn’t throw it?
    Do you have any ideas about this??
  • should I include the text “Queen” in my output ?
  • is it that I should call a different function “Queen” for black and white to decide if they are on board and treat the “throw” if not witch try catch?
  • at which point should the exception be thrown in a function Queen or when a struct Queen is defined or in the function canattack?

thanks for any answer

Nisang

Is that your full code?

As to your other questions, the test runner output is telling you it evaluated Queen(8, 4) on line 11 where it expected ExercismTestReports.InvalidPosition but got something else. Specifically, no error was raised where one was expected.

It’s notable that it’s calling Queen(8, 4) and expecting an error in the process. It’s not calling canattack() unless you call it yourself while Queen() is being evaluated so you can set that aside unless you are calling that function.

What is black when you’re instantiating a Queen?

Okay,
now I see what you were up to!

And:
No, they aren’t…
they are Int64

And it seems this exercise is also about Multiple dispatch…
and/or constructors
without saying so… ?

As a practice exercise, it’s not specifically about multiple dispatch unless you make it part of your solution.

To rephrase my earlier question which will cut to the heart of the issue I promise, where in your code does Queen(8, 4) run? Does it run in that constructor function?

Dear Andras,

thanks for your answer,
I have to admit that I don’t really understand your question.
Do you mean where to (which is called by the compiler the function or the struct)?

I just solved it! …
Believing I used multiple dispatch and then
talked to my Julia friend
who said Queen() is a constructor function.
and with this it would be solveable very easily!
and also
letting me realize I have not noticed so far that is an own concept…

which is not part of the syllabus!?

I don’t know if I’d published my final code if that would spoil the solution to students following…?

have a good afternoon,

Nisang

That’s good. I’d strongly recommend you make a mentoring request and go over your solution with a mentor to make sure any issues are fully addressed. There was a bit more going on here than we addressed, but it’s great that you’re unstuck.

??
what do you mean?

You should request a code review on the website and discuss that with a mentor :slightly_smiling_face:

1 Like

it’s already done!
and I also started reading the constructor concept in the julia lang docs