asp.net - Showing the error "A previous catch clause already catches all exceptions of this or a super type `System.Exception' " in my C# code -
showing error "a previous catch clause catch clause catches exceptions of or super type `system.exception' " in c# code
using system;    class test {      static void main()  {          try{              int a=10,b=0,c=0;c=a/b ;              console.writeline(c);         }            catch(system.exception e) {              console.writeline(e.message);          }          catch(system.dividebyzeroexception ex) {               console.writeline(ex.message);          }      }  } 
exception handlers processed in order top bottom , first matching exception handler invoked. because first handler catches system.exception, , exceptions derive system.exception, it'll catch everything, , second handler never execute.
the best practice multiple exception handlers order them specific general, this:
using system;    class test {      static void main()  {          try{              int a=10,b=0,c=0;c=a/b ;              console.writeline(c);         }            catch(system.dividebyzeroexception ex) {               console.writeline(ex.message);          }          catch(system.exception e) {              console.writeline(e.message);          }      }  } if absolutely positively have handle system.exception first (although can't think of reason why) write exception filter allow dividebyzero through, this:
using system;    class test {      static void main()  {          try{              int a=10,b=0,c=0;c=a/b ;              console.writeline(c);         }            catch(system.exception e)          when (!(e dividebyzeroexception)){              console.writeline(e.message);          }          catch(system.dividebyzeroexception ex) {               console.writeline(ex.message);          }      }  } note: per msdn, you should avoid catching general exception types system.exception.
Comments
Post a Comment