locked
Throwing exceptions RRS feed

  • Question

  • User893574298 posted


    Some Syntax questions here;



    try
    {
       If(condition){
       }
       else{throw new Exception("bla bla" ) 
       }  
       If(condition){
       }
       else{throw new Exception("bla bla n2" ) 
       }  
    }
     catch (Exception Excpt)
    {
    // LOG Excpt
    }



    would this be correct in terms of throwing exceptions :P

    Monday, May 17, 2010 6:10 AM

Answers

  • User-37275327 posted

    simply

    try

    {

       //Your if conditions here

    }

    Catch (Exeption ex)

    {

       // catch ur exeption here

    }

    throwing exception in every if condition make more complications in large enterprise applications. Simple try..catch is simply enough if there is no unconditional behaviour to catch. Other advantage you can keep the consistency in your application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2010 10:01 AM
  • User-1102123764 posted

    Hi,

    so syntacticly would the syntax of below be correct ?
     

    Yes.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 19, 2010 1:43 AM

All replies

  • User893574298 posted

    ?

    Monday, May 17, 2010 9:03 AM
  • User-37275327 posted

    simply

    try

    {

       //Your if conditions here

    }

    Catch (Exeption ex)

    {

       // catch ur exeption here

    }

    throwing exception in every if condition make more complications in large enterprise applications. Simple try..catch is simply enough if there is no unconditional behaviour to catch. Other advantage you can keep the consistency in your application.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 17, 2010 10:01 AM
  • User893574298 posted

    hi thanks for replying

    im adding error logging to a project so its not a case of beinging and to do better practices.. im an intern and havent really done error logging that much hence the syntax question..

    in the (cs) pages im adding this logging, ive been told to keep the database logging count down hence throwing the error up for it to be captured.

    so syntacticly would the syntax of below be correct ?

    pageLoad function
    {
    try{
    funtion1();
    }
     catch (Exception Excpt)
    {
    // Log Excpt 
    //display Excpt message.
    }
    
    funtion1
    try
    {
     if(condition)
     { 
       if(condition) 
       { }
       else{throw new exception ("error 2");}
      }
     else{throw new exception ("error 1");}
    }
    catch (Exception Excpt)
    {
    throw new Exception("funtion1()" +  Excpt.message);
    }
    
    
    



    Monday, May 17, 2010 10:22 AM
  • User-37275327 posted

    It is wise to put your exception handling class to use on catch section. Create exeption handling class and send the loggedusername, error desription, datetime, and code method to the database.


    Monday, May 17, 2010 11:38 AM
  • User-1102123764 posted

    Hi,

    so syntacticly would the syntax of below be correct ?
     

    Yes.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 19, 2010 1:43 AM
  • User893574298 posted

    thanks, all 35 pages now error & event coded with logging

    Tuesday, June 1, 2010 5:11 AM
  • User-1102123764 posted

    Hi,

    What's the error message? What is your real requirement? 

    Tuesday, June 1, 2010 5:24 AM
  • User893574298 posted

    We dont display any errors to the user on screen should one happen, we have a message displaying a code the code represents the error but only we hold the codes the idea being to not show anything to the users we log all errors to identify probelms quicker and log events for future reports.

    anyways done now thanks.


    Tuesday, June 1, 2010 5:33 AM
  • User-446933086 posted
    using System;
    public class EHClass 
    {
       public static void Main () 
       {
          try 
          {
             Console.WriteLine("Executing the try statement.");
             throw new NullReferenceException();
          }
    
          catch(NullReferenceException e) 
          {
             Console.WriteLine("{0} Caught exception #1.", e); 
          }
    
          catch 
          {
             Console.WriteLine("Caught exception #2.");
          }
    
          finally 
          {
             Console.WriteLine("Executing finally block.");
          }
       }
    }
    Please visit http://msdn.microsoft.com/en-us/library/dszsf989(VS.71).aspx
    Tuesday, June 1, 2010 7:14 AM