Skip to content

Commit 526e8e3

Browse files
Added Custom exception to flag a model rule violation
1 parent 3c84d28 commit 526e8e3

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="SysMl2ModelException.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2026 Starion Group S.A.
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// </copyright>
19+
// ------------------------------------------------------------------------------------------------
20+
21+
namespace SysML2.NET.Exceptions
22+
{
23+
using System;
24+
25+
/// <summary>
26+
/// The <see cref="SysMl2ModelException"/> provide custom exception that should be used when a SysML 2 rule violation appears into a model
27+
/// </summary>
28+
public class SysMl2ModelException: Exception
29+
{
30+
/// <summary>Initializes a new instance of the <see cref="SysMl2ModelException" /> class.</summary>
31+
public SysMl2ModelException()
32+
{
33+
}
34+
35+
/// <summary>Initializes a new instance of the <see cref="SysMl2ModelException" /> class with a specified error message.</summary>
36+
/// <param name="message">The message that describes the error.</param>
37+
public SysMl2ModelException(string message) : base(message)
38+
{
39+
}
40+
41+
/// <summary>Initializes a new instance of the <see cref="SysMl2ModelException" /> class with a specified error message and a reference to the inner exception that is the cause of this exception.</summary>
42+
/// <param name="message">The error message that explains the reason for the exception.</param>
43+
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (<see langword="Nothing" /> in Visual Basic) if no inner exception is specified.</param>
44+
public SysMl2ModelException(string message, Exception innerException) : base(message, innerException)
45+
{
46+
}
47+
}
48+
}

SysML2.NET/Extend/OwningMembershipExtensions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace SysML2.NET.Core.POCO.Root.Namespaces
2727
using SysML2.NET.Core.Root.Namespaces;
2828
using SysML2.NET.Core.POCO.Root.Annotations;
2929
using SysML2.NET.Core.POCO.Root.Elements;
30+
using SysML2.NET.Exceptions;
3031

3132
/// <summary>
3233
/// The <see cref="OwningMembershipExtensions"/> class provides extensions methods for
@@ -45,7 +46,12 @@ internal static class OwningMembershipExtensions
4546
/// </returns>
4647
internal static IElement ComputeOwnedMemberElement(this IOwningMembership owningMembershipSubject)
4748
{
48-
return owningMembershipSubject == null ? throw new ArgumentNullException(nameof(owningMembershipSubject)) : owningMembershipSubject.OwnedRelatedElement.Single();
49+
if (owningMembershipSubject == null)
50+
{
51+
throw new ArgumentNullException(nameof(owningMembershipSubject));
52+
}
53+
54+
return owningMembershipSubject.OwnedRelatedElement.Count != 1 ? throw new SysMl2ModelException($"{nameof(owningMembershipSubject)} must have exactly one related element") : owningMembershipSubject.OwnedRelatedElement.Single();
4955
}
5056

5157
/// <summary>

0 commit comments

Comments
 (0)