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 219 views
219 views asked Mar 20, 2025 by avibootz
2 answers 262 views
262 views asked Nov 9, 2023 by avibootz
1 answer 195 views
195 views asked Aug 8, 2024 by avibootz
1 answer 216 views
216 views asked Aug 13, 2023 by avibootz
1 answer 242 views
1 answer 288 views
288 views asked Apr 21, 2017 by avibootz
...