473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Throw new Exception()

184 100+
Hi,
In the following example i'm throwing new exception from "try" block.But i havent seen the thrown exception anywhere while running the program.Eventhough there is "Finally" block ,the exception should be thrown and "finally" block should be executed.Here it was not so,because the exception is not thrown.can anyone please help me. please correct me if i'm wrong.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Test3{
  3.     public static void main(String args[]){
  4.         System.out.println(method());
  5.     }
  6.     public static int method(){
  7.         try{
  8.             throw new Exception();
  9.         }
  10.         catch(Exception e){
  11.             throw new Exception();
  12.         }
  13.         finally{
  14.             return 3;
  15.         }
  16.     }
  17. }
  18.  
  19.  
  20.  
  21.  
-Thanks & Regards,
Hamsa
Mar 18 '08 #1
5 5369
Nepomuk
3,112 Expert 2GB
Hi,
In the following example i'm throwing new exception from "try" block.But i havent seen the thrown exception anywhere while running the program.Eventhough there is "Finally" block ,the exception should be thrown and "finally" block should be executed.Here it was not so,because the exception is not thrown.can anyone please help me. please correct me if i'm wrong.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Test3{
  3.     public static void main(String args[]){
  4.         System.out.println(method());
  5.     }
  6.     public static int method(){
  7.         try{
  8.             throw new Exception();
  9.         }
  10.         catch(Exception e){
  11.             throw new Exception();
  12.         }
  13.         finally{
  14.             return 3;
  15.         }
  16.     }
  17. }
  18.  
  19.  
-Thanks & Regards,
Hamsa
There's something strange about your catch block there... So tell me, what exactly happenes when a Exception is thrown?
Greetings,
Nepomuk
Mar 25 '08 #2
talonx
18
Hi,
In the following example i'm throwing new exception from "try" block.But i havent seen the thrown exception anywhere while running the program.Eventhough there is "Finally" block ,the exception should be thrown and "finally" block should be executed.Here it was not so,because the exception is not thrown.can anyone please help me. please correct me if i'm wrong.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Test3{
  3.     public static void main(String args[]){
  4.         System.out.println(method());
  5.     }
  6.     public static int method(){
  7.         try{
  8.             throw new Exception();
  9.         }
  10.         catch(Exception e){
  11.             throw new Exception();
  12.         }
  13.         finally{
  14.             return 3;
  15.         }
  16.     }
  17. }
  18.  
  19.  
  20.  
  21.  
-Thanks & Regards,
Hamsa
You have committed a very common mistake that beginners do - returning from a finally block. Since its the last thing to be executed before it returns from the method, the exception thrown , or anything else returned, is lost.
Mar 25 '08 #3
JosAH
11,448 Expert 8TB
I can't help it but that catch block reminds me of a short stop in a baseball game:
catch the ball and throw it away as fast as possible again.

kind regards,

Jos ;-)
Mar 25 '08 #4
Nepomuk
3,112 Expert 2GB
I can't help it but that catch block reminds me of a short stop in a baseball game:
catch the ball and throw it away as fast as possible again.

kind regards,

Jos ;-)
Actually, it's like catching a ball and throwing a different ball away as fast as possible... ^^

Greetings,
Nepomuk
Mar 27 '08 #5
JosAH
11,448 Expert 8TB
Actually, it's like catching a ball and throwing a different ball away as fast as possible... ^^

Greetings,
Nepomuk
Yep, but in this particular example a 'finally block' comes in and returns 3
before the other party can catch my new ball.

kind regards,

Jos
Mar 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: arun gunda | last post by:
I want to throw exception dynamically. This what I want to do For example I want to throw System.Net.WebException exception. I will know the full exception name at run time, can I create a...
3
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
2
by: Dave | last post by:
Josuttis states that I may not throw an exception of type exception or of one of the standard exception types used for language support. Where in the Standard am I forbidden from "throw...
2
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
1
by: z. f. | last post by:
in vb asp.net page i'm overriding the finalize method in order to make cleanup. if i throw exception there it is not seen on the page. probably because the page has already sent to the client. is...
3
by: Ryan Liu | last post by:
Hi, In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges method will throw RowNotInTableException exeception. And in DataTable.AcceptChanges(), the documentation does not...
5
by: Rob Dob | last post by:
I am trying to set the NullValue within the Column properties of my Dataset in VS2005. The DataType is a System.DateTime. and when I try and change it from "(Throw Exception)" I get the following...
0
by: Steve B. | last post by:
Hi, I'm wondering how to correctly throw exception within ASP.Net pages. I've page wich which waits for an "id" parameter in the querystring. I want to validate this param. I've wrote this...
1
by: =?Utf-8?B?TVIgRQ==?= | last post by:
This may seem like a stupid question but in C#: Say for instance I have a set of SQL processes that I run via ExecuteReader(). These processes return several pieces of information to the...
4
by: George2 | last post by:
Hello everyone, In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector. In what situation will sort throw exception? I can not find a case....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.