474,047 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

custom Throw() function

Hi,
I have function which is throwning exception, like this:

public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}

I am using like this:

public int fun()
{
if (..)
return 0;
(new LoggedException ()).Throw(); <-- Compiler Error CS0161
}

But I get 'Compiler Error CS0161: not all code paths return a value'.
How can I tell to compiler that I don't need return value when I call
Throw() function?
Thank you,
Nenad
Nov 16 '05 #1
3 2373
Nenad,

You can not. The compiler doesn't know that the Throw method will
always throw an exception, so you need to place a return statement after the
call to Throw.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Nenad Dobrilovic" <ch*******@hotm ail.com> wrote in message
news:eN******** ******@TK2MSFTN GP09.phx.gbl...
Hi,
I have function which is throwning exception, like this:

public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}

I am using like this:

public int fun()
{
if (..)
return 0;
(new LoggedException ()).Throw(); <-- Compiler Error CS0161
}

But I get 'Compiler Error CS0161: not all code paths return a value'.
How can I tell to compiler that I don't need return value when I call
Throw() function?
Thank you,
Nenad

Nov 16 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Nenad,

You can not. The compiler doesn't know that the Throw method will
always throw an exception, so you need to place a return statement after the
call to Throw.

Hope this helps.


Thank you for your quick answer.
I was hoping that I can tell him, beacuse he is not clever enough to
find out by himself.
Nov 16 '05 #3
Nenad Dobrilovic wrote:
Hi,
I have function which is throwning exception, like this:

public class LoggedException : Exception
{
public void Throw()
{
throw this;
}
}

I am using like this:

public int fun()
{
if (..)
return 0;
(new LoggedException ()).Throw(); <-- Compiler Error CS0161
}

But I get 'Compiler Error CS0161: not all code paths return a value'.
How can I tell to compiler that I don't need return value when I call
Throw() function?


You have several options:

//Option 1:
public int fun()
{
if (!(..)) {
(new LoggedException ()).Throw();
}

// ...
return 0;
}
// Option 2:
public class LoggedException : Exception
{
public LoggedException GetException()
{
return( this);
}
}
public int fun()
{
if (..)
return 0;
throw (new LoggedException ()).GetExceptio n();
}

//Option 3:
// put whatever logging functionality you would have put
// into Throw() (or GetException()) into the
// LoggedException () constructor and simply do:

throw new LoggedException ();

--
mikeb
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1486
by: Nick | last post by:
My client uses a SQL Database to store their usernames and passwords, and I do not believe they have AD...no big deal... I wrote a class to create a generic identity and generic principal so that I can use the .IsInRole function for some added security. I would like to do the same by applying an attribute to a method or class. The code I am including works from what I can see, but I am experiencing the following... 1) I cannot add the...
1
8503
by: Craig | last post by:
I have added a 'Textboxes (A)' to my UI installer project along with a custom action to pass the value back to a class I've written to override the void Install function. As long as the text is very simple everything is working just fine. I've tested it by writing the string out to a text file to test it. However, the trouble comes in when I use the textbox for what I really intended, a ADO.Net Connection String. The textbox is to...
8
3214
by: Tinus | last post by:
Hello all, I've create a custom control (UserControl) and have a custom Item Collection. The control is a custom calendar which is draw using the Graphics Rectangle etc. functions. It is drawn when the control is painted or resized. When the control is drawn it draws also the items found in the collection. So far so good.... I have 3 questions which I'm unable to find a solution
2
3326
by: S. Justin Gengo | last post by:
Hi, I've created a component that allows me to store database information for various types of databases my company uses. It uses a collection for each type of database. Everything is working perfectly except when the component is deleted from the page. Here are the details. I add the component to the page from the toolbox and then add items using the collection editor. I can delete those items from the collection using the collection...
0
3012
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to implement those buttons, but I'm not sure what the code should look like. I tried what I thought was obvious code: myCollection.AddNew() and
3
2350
by: DC Gringo | last post by:
Hi, I'm trying to use a custom action to modify a database (rather than create one) using the VS.NET '03's help example called "Custom Action to Create Database During Installation". I've made two modifications to the sample in the document...both are in the "Protected Sub AddDBTable" (towards the bottom). I've changed ' Creates the database.
6
1599
by: Steve Amey | last post by:
Hi all I want to be able to throw a custom error up the call stack. I have looked around and it seems as though it's possible, but I can't get it to work :o( Below is some sample code. ------------------------------------------------------------ Public Class MainForm Public Sub Show Form Try
1
5023
by: Jakob Lithner | last post by:
When I started a new ASP project I was eager to use the login facilities offered in Framework 2.0/VS 2005. I wanted: - A custom principal that could hold my integer UserID from the database - An easy way to classify different pages as either Admin, Member or Public, where login is necessary for Admin and Member but not for Public. My idea was to put the pages in different directories to easily keep my order. - An easy menu system that...
3
3426
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own exception constructors? I've noticed that, f.ex., ArgumentException accepts null arguments without raising ArgumentNullException. Obviously, if nothing is to be supplied to the exception constructor, the default constructor should
3
20266
by: =?Utf-8?B?Vk1J?= | last post by:
How can I catch an error and throw the exception message plus some custom text? For example, I'd like to do something like this: try { //generate error } catch { //throw ex + " my custom message"; // OR
0
10350
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
12151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11607
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11145
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8704
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4947
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.