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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

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

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use try catch throw in C#

2 Answers

0 votes
using System;
  
class Program
{
    static void test(string s) {
        if (s == null) {
            throw new ArgumentNullException();
        }
    }
  
    static void Main()
    {
        string s = null; 
  
        try { 
            test(s);
        }
        catch (Exception ex) {
            Console.WriteLine("catch (Exception ex): {0}", ex);
        }
    }
}
  
  
  
  
/*
run:
  
catch (Exception ex): System.ArgumentNullException: Value cannot be null.
  at Program.Main () [0x00002] in <06380fb13dc741b4b4b44fee5649d698>:0 
  
*/

 



answered Nov 22, 2020 by avibootz
edited Nov 23, 2020 by avibootz
0 votes
using System;
 
class Program
{
    static void test(string s) {
        if (s == null) {
            throw new ArgumentNullException();
        }
    }
 
    static void Main()
    {
        try {
            string s = null;
            test(s);
        }
        catch (ArgumentNullException ex) {
            Console.WriteLine("catch (ArgumentNullException ex): {0}", ex);
        }
        catch (Exception ex) {
            Console.WriteLine("catch (Exception ex): {0}", ex);
        }
    }
}
 
 
 
 
/*
run:
 
catch (ArgumentNullException ex): System.ArgumentNullException: Value cannot be null.
  at Program.Main () [0x00002] in <d1d2cd0e27104097b286d3b2e5edcf4a>:0 
 
*/

 



answered Nov 23, 2020 by avibootz
edited Nov 23, 2020 by avibootz

Related questions

1 answer 116 views
116 views asked Oct 8, 2022 by avibootz
2 answers 268 views
3 answers 298 views
298 views asked Jan 3, 2016 by avibootz
1 answer 228 views
4 answers 307 views
1 answer 191 views
191 views asked Nov 23, 2020 by avibootz
...