|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Exception handlingI've never been any good at handling exceptions... So I need your help. Within a try{ } block, I have to call a function that has to throw its own exception. How should I go about doing this? try { checkNullHeader( sHeader, ref status ); // ... } // ... // definition of checkNullHeader void checkNullHeader( AuthHeader sHeader, ref status status ) { // if token is null or empty, do not even try to authenticate if( sHeader == null || sHeader.Token == null || sHeader.Token.Equals( "" ) ) { try { status.Code = Status.StatusCode.UNAUTHORIZED; status.Description = "Unauthorized"; throw new Exception( "Unauthorized" ); } catch( Exception e ) { Console.WriteLine( "Exception caught here:" + e.ToString ( ) ); } } } Is something like this legal? Am I even throwing the exception correctly? (Seriously. Exceptions are very foreign to me.) Help me! Thanks. Allie Allie wrote:
Show quote > Hi, all. Since you are catching the exception thrown within checkNullHeader in > > I've never been any good at handling exceptions... So I need your > help. > > Within a try{ } block, I have to call a function that has to throw its > own exception. How should I go about doing this? > > try { > checkNullHeader( sHeader, ref status ); > // ... > } > > // ... > > // definition of checkNullHeader > void checkNullHeader( AuthHeader sHeader, ref status status ) > { > // if token is null or empty, do not even try to authenticate > if( sHeader == null || sHeader.Token == null || > sHeader.Token.Equals( "" ) ) > { > try { > status.Code = Status.StatusCode.UNAUTHORIZED; > status.Description = "Unauthorized"; > throw new Exception( "Unauthorized" ); > } > catch( Exception e ) > { > Console.WriteLine( "Exception caught here:" + e.ToString ( ) ); > } > } > } > > Is something like this legal? Am I even throwing the exception > correctly? (Seriously. Exceptions are very foreign to me.) Help me! that same function, your outside call will not see the "Unauthorized" exception. If you simply want to log the "Unauthorized" condition (you're already setting a status code and description so the caller to checkNullHeader can inspect those to determine the action to take, the throwing/catching of an exception with checkNullHeader is a waste. Can you better define your design? -- Tom Porterfield On Nov 28, 4:57 pm, Tom Porterfield <tppor***@mvps.org> wrote:
Show quote > Allie wrote: So if this function (checkNullHeader) is defined within the try{ }> > Hi, all. > > > I've never been any good at handling exceptions... So I need your > > help. > > > Within a try{ } block, I have to call a function that has to throw its > > own exception. How should I go about doing this? > > > try { > > checkNullHeader( sHeader, ref status ); > > // ... > > } > > > // ... > > > // definition of checkNullHeader > > void checkNullHeader( AuthHeader sHeader, ref status status ) > > { > > // if token is null or empty, do not even try to authenticate > > if( sHeader == null || sHeader.Token == null || > > sHeader.Token.Equals( "" ) ) > > { > > try { > > status.Code = Status.StatusCode.UNAUTHORIZED; > > status.Description = "Unauthorized"; > > throw new Exception( "Unauthorized" ); > > } > > catch( Exception e ) > > { > > Console.WriteLine( "Exception caught here:" + e.ToString ( ) ); > > } > > } > > } > > > Is something like this legal? Am I even throwing the exception > > correctly? (Seriously. Exceptions are very foreign to me.) Help me! > > Since you are catching the exception thrown within checkNullHeader in > that same function, your outside call will not see the "Unauthorized" > exception. If you simply want to log the "Unauthorized" condition > (you're already setting a status code and description so the caller to > checkNullHeader can inspect those to determine the action to take, the > throwing/catching of an exception with checkNullHeader is a waste. > > Can you better define your design? > -- > Tom Porterfield block of a particular class, it does not need to throw/catch an exception? (Is it OK to leave throw new Exception( "Unauthorized" ); in the function?) What if I want to access checkNullHeader from other classes... and/or not necessarily from within a try{ } block? Will a try-catch block within checkNullHeader make sense then? An exception needs to be thrown because, while the code and description of the status are changing, the calling function is not using this data to determine a course of action. If the user is not authorized, the rest of the function should not execute. At least, that's how I understand it. (I did not write the code. My job is to take if( sHeader == null || sHeader.Token == null || sHeader.Token.Equals( "" ) ) { .... } and turn it into a function, as it is used in a lot of other functions.) Thanks, Allie Allie wrote:
Show quote > A good general rule of thumb is that if you can handle the situation > So if this function (checkNullHeader) is defined within the try{ } > block of a particular class, it does not need to throw/catch an > exception? (Is it OK to leave throw new Exception( "Unauthorized" ); > in the function?) > > What if I want to access checkNullHeader from other classes... and/or > not necessarily from within a try{ } block? Will a try-catch block > within checkNullHeader make sense then? > > An exception needs to be thrown because, while the code and > description of the status are changing, the calling function is not > using this data to determine a course of action. If the user is not > authorized, the rest of the function should not execute. At least, > that's how I understand it. (I did not write the code. My job is to > take > > if( sHeader == null || sHeader.Token == null || > sHeader.Token.Equals( "" ) ) { > ... > } > > and turn it into a function, as it is used in a lot of other > functions.) through logic then you shouldn't throw an exception. So I would code up a function called isHeaderNull that returns a true if the header is null, otherwise false. Then the caller of the function need only check a simple bool value and act accordingly. -- Tom Porterfield |
|||||||||||||||||||||||