C
C#3mo ago
Theos

✅ Throw error without showing the call stack

Hey, so I'm working on a toy compiler and I want to throw errors when compiler detects some issues thats how it currently looks: (look at the screenshot) And thats the code:
public class CustomException : Exception
{
public CustomException(string message) : base(message) { }
}

public class ErrorHandler
{
public static void Custom(string message)
{
throw new CustomException(message);
}

public static void Expected(string message, int line)
{
throw new CustomException($"[{line}] Expected '{message}'!");
}
}
public class CustomException : Exception
{
public CustomException(string message) : base(message) { }
}

public class ErrorHandler
{
public static void Custom(string message)
{
throw new CustomException(message);
}

public static void Expected(string message, int line)
{
throw new CustomException($"[{line}] Expected '{message}'!");
}
}
What I want to do is only print the message and stop the program. I don't want to print Unhlandled exception or this call stack:
at ils.ErrorHandler.Custom(String message) in C:\Projects\ils.ils\ils\ErrorHandler.cs:line 18
at ils.Verificator.VariableDuplicates(ASTScope scope) in C:\Projects\ils.ils\ils\Verificator.cs:line 34
at ils.Verificator.Verify(ASTScope mainScope) in C:\Projects\ils.ils\ils\Verificator.cs:line 13
at ILS.Main(String[] args) in C:\Projects\ils.ils\ils\ils.cs:line 33
at ils.ErrorHandler.Custom(String message) in C:\Projects\ils.ils\ils\ErrorHandler.cs:line 18
at ils.Verificator.VariableDuplicates(ASTScope scope) in C:\Projects\ils.ils\ils\Verificator.cs:line 34
at ils.Verificator.Verify(ASTScope mainScope) in C:\Projects\ils.ils\ils\Verificator.cs:line 13
at ILS.Main(String[] args) in C:\Projects\ils.ils\ils\ils.cs:line 33
No description
1 Reply
Theos
Theos3mo ago
How can this be achieved? okay, I just remembered that Environment.Exit exists...
private static void Log(string message)
{
Console.WriteLine($"\n{message}");
Environment.Exit(-1);
}

public static void Custom(string message)
{
Log(message);
}

public static void Expected(string message, int line)
{
Log($"[{line}] Expected '{message}'!");
}
private static void Log(string message)
{
Console.WriteLine($"\n{message}");
Environment.Exit(-1);
}

public static void Custom(string message)
{
Log(message);
}

public static void Expected(string message, int line)
{
Log($"[{line}] Expected '{message}'!");
}