Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

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 168 views
168 views asked Mar 20, 2025 by avibootz
2 answers 197 views
197 views asked Nov 9, 2023 by avibootz
1 answer 153 views
153 views asked Aug 8, 2024 by avibootz
1 answer 158 views
158 views asked Aug 13, 2023 by avibootz
1 answer 183 views
1 answer 234 views
234 views asked Apr 21, 2017 by avibootz
...