How to exit a method in C#

2 Answers

0 votes
using System;

class Program
{
    static void Main(string[] args)
    {
        try {
            Console.WriteLine(function(3)); 
            Console.WriteLine(function(0)); 
        }
        catch (ArgumentException ex) {
            Console.WriteLine("Err: " + ex.Message);
        }
    }

    public static int function(int n) {
        if (n == 0) {    
            throw new ArgumentException("The value 0 is not accepted.");
        }
        
        return n * 100;       
    }
}



/*
run:

300
Err: The value 0 is not accepted.

*/

 



answered Mar 20, 2025 by avibootz
0 votes
using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(function(3)); 
        Console.WriteLine(function(0)); 
    }

    public static int function(int n) {
        if (n == 0) {    
            Console.WriteLine("Err: The value 0 is not accepted. Returning -1.");
            return -1;
        }
        
        return n * 100;       
    }
}



/*
run:

300
Err: The value 0 is not accepted. Returning -1.
-1

*/

 



answered Mar 20, 2025 by avibootz

Related questions

2 answers 214 views
214 views asked Mar 20, 2025 by avibootz
2 answers 254 views
254 views asked Nov 9, 2023 by avibootz
1 answer 189 views
189 views asked Aug 8, 2024 by avibootz
1 answer 210 views
210 views asked Aug 13, 2023 by avibootz
1 answer 238 views
1 answer 285 views
285 views asked Apr 21, 2017 by avibootz
...