In this chapter we will discuss exceptions in the object-oriented programming and in C# in particular. We will learn how to handle exceptions using the try-catch construct, how to pass them to the calling methods and how to throw standard or our own exceptions using the throw construct. We will give various examples for using exceptions. We will look at the types of exceptions and the exceptions hierarchy in the .NET Framework. At the end, we will look at the advantages of using exceptions, best practices and how to apply them in different situations.
In this chapter we will discuss exceptions in the object-oriented programming and in Java in particular. We will learn how to handle exceptions using the try-catch construct, how to pass them to the calling methods and how to throw standard or our own exceptions using the throw construct.
In this chapter we will discuss exceptions in the object-oriented programming and in C# in particular. We will learn how to handle exceptions using the try-catch construct, how to pass them to the calling methods and how to throw standard or our own exceptions using the throw construct. We will give various examples for using exceptions. We will look at the types of exceptions and the exceptions hierarchy in the .NET Framework. At the end, we will look at the advantages of using exceptions, best practices and how to apply them in different situations.
In this chapter we will discuss exceptions in the object-oriented programming and in Java in particular. We will learn how to handle exceptions using the try-catch construct, how to pass them to the calling methods and how to throw standard or our own exceptions using the throw construct.
Exceptions are a powerful mechanism for centralized processing of errors and exceptional situations. This mechanism replaces the procedure-oriented method of error handling in which each function returns a code indicating an error or a successful execution.
JAVA EXCEPTION HANDLING
N.V.Raja Sekhar Reddy
www.technolamp.co.in
Want more interesting...
Watch and Like us @ https://www.facebook.com/Technolamp.co.in
subscribe videos @ http://www.youtube.com/user/nvrajasekhar
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Exceptions are a powerful mechanism for centralized processing of errors and exceptional situations. This mechanism replaces the procedure-oriented method of error handling in which each function returns a code indicating an error or a successful execution.
JAVA EXCEPTION HANDLING
N.V.Raja Sekhar Reddy
www.technolamp.co.in
Want more interesting...
Watch and Like us @ https://www.facebook.com/Technolamp.co.in
subscribe videos @ http://www.youtube.com/user/nvrajasekhar
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
About
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Technical Specifications
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
Key Features
Indigenized remote control interface card suitable for MAFI system CCR equipment. Compatible for IDM8000 CCR. Backplane mounted serial and TCP/Ethernet communication module for CCR remote access. IDM 8000 CCR remote control on serial and TCP protocol.
• Remote control: Parallel or serial interface
• Compatible with MAFI CCR system
• Copatiable with IDM8000 CCR
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
Application
• Remote control: Parallel or serial interface.
• Compatible with MAFI CCR system.
• Compatible with IDM8000 CCR.
• Compatible with Backplane mount serial communication.
• Compatible with commercial and Defence aviation CCR system.
• Remote control system for accessing CCR and allied system over serial or TCP.
• Indigenized local Support/presence in India.
• Easy in configuration using DIP switches.
Immunizing Image Classifiers Against Localized Adversary Attacksgerogepatton
This paper addresses the vulnerability of deep learning models, particularly convolutional neural networks
(CNN)s, to adversarial attacks and presents a proactive training technique designed to counter them. We
introduce a novel volumization algorithm, which transforms 2D images into 3D volumetric representations.
When combined with 3D convolution and deep curriculum learning optimization (CLO), itsignificantly improves
the immunity of models against localized universal attacks by up to 40%. We evaluate our proposed approach
using contemporary CNN architectures and the modified Canadian Institute for Advanced Research (CIFAR-10
and CIFAR-100) and ImageNet Large Scale Visual Recognition Challenge (ILSVRC12) datasets, showcasing
accuracy improvements over previous techniques. The results indicate that the combination of the volumetric
input and curriculum learning holds significant promise for mitigating adversarial attacks without necessitating
adversary training.
Student information management system project report ii.pdfKamal Acharya
Our project explains about the student management. This project mainly explains the various actions related to student details. This project shows some ease in adding, editing and deleting the student details. It also provides a less time consuming process for viewing, adding, editing and deleting the marks of the students.
Welcome to WIPAC Monthly the magazine brought to you by the LinkedIn Group Water Industry Process Automation & Control.
In this month's edition, along with this month's industry news to celebrate the 13 years since the group was created we have articles including
A case study of the used of Advanced Process Control at the Wastewater Treatment works at Lleida in Spain
A look back on an article on smart wastewater networks in order to see how the industry has measured up in the interim around the adoption of Digital Transformation in the Water Industry.
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Explore the innovative world of trenchless pipe repair with our comprehensive guide, "The Benefits and Techniques of Trenchless Pipe Repair." This document delves into the modern methods of repairing underground pipes without the need for extensive excavation, highlighting the numerous advantages and the latest techniques used in the industry.
Learn about the cost savings, reduced environmental impact, and minimal disruption associated with trenchless technology. Discover detailed explanations of popular techniques such as pipe bursting, cured-in-place pipe (CIPP) lining, and directional drilling. Understand how these methods can be applied to various types of infrastructure, from residential plumbing to large-scale municipal systems.
Ideal for homeowners, contractors, engineers, and anyone interested in modern plumbing solutions, this guide provides valuable insights into why trenchless pipe repair is becoming the preferred choice for pipe rehabilitation. Stay informed about the latest advancements and best practices in the field.
2. What are Exceptions?
Handling Exceptions
The System.Exception Class
Types of Exceptions and their Hierarchy
Raising (Throwing) Exceptions
Best Practices
2
3. WHAT ARE EXCEPTIONS?
The exceptions in .NET Framework are classic
implementation of the OOP exception model
Deliver powerful mechanism for centralized handling of
errors and unusual events
Substitute procedure-oriented approach,
in which each function returns error code
Simplify code construction and maintenance
Allow the problematic situations to be
processed at multiple levels
3
4. HANDLING EXCEPTIONS
In C# the exceptions can be handled by the try-
catch-finally construction
catch blocks can be used multiple times to
process different exception types
4
try
{
// Do some work that can raise an exception
}
catch (DivideByZeroException ex)
{
// Handle the caught DivideByZeroException ex
}
Catch (Exeption ex)
{
// Handle the caught Exception ex
}
5. HANDLING EXCEPTIONS –
EXAMPLE
5
static void Main()
{
string s = Console.ReadLine();
try
{
Int32.Parse(s);
Console.WriteLine(
"You entered valid Int32 number {0}.", s);
}
catch (FormatException)
{
Console.WriteLine("Invalid integer number!");
}
catch (OverflowException)
{
Console.WriteLine(
"The number is too big to fit in Int32!");
}
}
6. THE SYSTEM.EXCEPTION
CLASS
Exception is a base class for all exceptions
Important properties:
Message – user-oriented message about error
Source – name of an error source (application or object)
InnerException – inner exception (if called from other)
StackTrace – call stack to the point of exception call
TargetSite – method name which raised an exception
HelpLink – URL-address to information about exception
Data – dictionary with additional information with exception
(IDictionary)
6
8. EXCEPTION PROPERTIES
The Message property gives brief
description of the problem
The StackTrace property is extremely
useful when identifying the reason
caused the exception
8
Exception caught: Input string was not in a correct
format.
at System.Number.ParseInt32(String s, NumberStyles
style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at ExceptionsTest.CauseFormatException() in
c:consoleapplication1exceptionstest.cs:line 8
at ExceptionsTest.Main(String[] args) in
c:consoleapplication1exceptionstest.cs:line 15
9. EXCEPTION PROPERTIES (2)
File names and line numbers are
accessible only if the compilation was in
Debug mode
When compiled in Release mode, the
information in the property StackTrace
is quite different:
9
Exception caught: Input string was not in a correct
format.
at System.Number.ParseInt32(String s, NumberStyles
style, NumberFormatInfo info)
at ExceptionsTest.Main(String[] args)
11. TYPES OF EXCEPTIONS
.NET exceptions inherit from System.Exception
The system exceptions inherit from
System.SystemException, e.g.
System.ArgumentException
System.NullReferenceException
System.OutOfMemoryException
System.StackOverflowException
User-defined exceptions should inherit from
System.ApplicationException
11
12. HANDLING EXCEPTIONS
When catching an exception of a particular class, all
its inheritors (child exceptions) are caught too
Example:
Handles ArithmeticException and its descendants
DivideByZeroException and OverflowException
12
try
{
// Do some works that can cause an exception
}
catch (System.ArithmeticException)
{
// Handle the caught arithmetic exception
}
13. FIND THE MISTAKE!
13
static void Main()
{
string s = Console.ReadLine();
try
{
Int32.Parse(s);
}
catch (Exception)
{
Console.WriteLine("Can not parse the number!");
}
catch (FormatException)
{
Console.WriteLine("Invalid integer number!");
}
catch (OverflowException)
{
Console.WriteLine(
"The number is too big to fit in Int32!");
}
}
This should be
last
Unreachable
code
Unreachable
code
14. HANDLING ALL EXCEPTIONS
All exceptions thrown by .NET managed code
inherit the System.Exception exception
Unmanaged code can throw other exceptions
For handling all exceptions (even unmanaged)
use the construction:
14
try
{
// Do some works that can raise any exception
}
catch
{
// Handle the caught exception
}
15. THROWING EXCEPTIONS
Exceptions are thrown (raised) by throw keyword in C#
Used to notify the calling code in case of error or unusual
situation
When an exception is thrown:
The program execution stops
The exception travels over the stack until a suitable catch block
is reached to handle it
Unhandled exceptions display
error message
15
17. USING THROW KEYWORD
Throwing an exception with an error message:
Exceptions can accept message and cause:
Note: if the original exception is not passed the initial cause of
the exception is lost
17
throw new ArgumentException("Invalid amount!");
try
{
Int32.Parse(str);
}
catch (FormatException fe)
{
throw new ArgumentException("Invalid number", fe);
}
18. RE-THROWING EXCEPTIONS
Caught exceptions can be re-thrown again:
18
try
{
Int32.Parse(str);
}
catch (FormatException fe)
{
Console.WriteLine("Parse failed!");
throw fe; // Re-throw the caught exception
}
catch (FormatException)
{
throw; // Re-throws the last caught exception
}
19. THROWING EXCEPTIONS –
EXAMPLE
19
public static double Sqrt(double value)
{
if (value < 0)
throw new System.ArgumentOutOfRangeException(
"Sqrt for negative numbers is undefined!");
return Math.Sqrt(value);
}
static void Main()
{
try
{
Sqrt(-1);
}
catch (ArgumentOutOfRangeException ex)
{
Console.Error.WriteLine("Error: " + ex.Message);
throw;
}
}
20. CHOOSING THE EXCEPTION
TYPE
When an invalid parameter is passed to a
method:
ArgumentException, ArgumentNullException,
ArgumentOutOfRangeException
When requested operation is not supported
NotSupportedException
When a method is still not implemented
NotImplementedException
If no suitable standard exception class is
available
Create own exception class (inherit Exception)
20
21. THE TRY-FINALLY STATEMENT
The statement:
Ensures execution of given block in all cases
When exception is raised or not in the try block
Used for execution of cleaning-up code, e.g. releasing
resources
21
try
{
// Do some work that can cause an exception
}
finally
{
// This block will always execute
}
22. TRY-FINALLY – EXAMPLE
22
static void TestTryFinally()
{
Console.WriteLine("Code executed before try-finally.");
try
{
string str = Console.ReadLine();
Int32.Parse(str);
Console.WriteLine("Parsing was successful.");
return; // Exit from the current method
}
catch (FormatException)
{
Console.WriteLine("Parsing failed!");
}
finally
{
Console.WriteLine(
"This cleanup code is always executed.");
}
Console.WriteLine(
"This code is after the try-finally block.");
}
23. EXCEPTIONS – BEST
PRACTICES
catch blocks should begin with the exceptions lowest
in the hierarchy
And continue with the more general exceptions
Otherwise a compilation error will occur
Each catch block should handle only these exceptions
which it expects
If a method is not competent to handle an exception, it should
be left unhandled
Handling all exceptions disregarding their type is popular bad
practice (anti-pattern)!
23
24. EXCEPTIONS – BEST PRACTICES
(2)
When raising an exception always pass to the
constructor good explanation message
When throwing an exception always pass a
good description of the problem
Exception message should explain what causes the
problem and how to solve it
Good: "Size should be integer in range [1…15]"
Good: "Invalid state. First call Initialize()"
Bad: "Unexpected error"
Bad: "Invalid argument"
24
25. EXCEPTIONS – BEST
PRACTICES (3)
Exceptions can decrease the application
performance
Throw exceptions only in situations which are
really exceptional and should be handled
Do not throw exceptions in the normal program
control flow (e.g. for invalid user input)
CLR could throw exceptions at any time with
no way to predict them
E.g. System.OutOfMemoryException
25
26. SUMMARY
Exceptions provide flexible error handling
mechanism in .NET Framework
Allow errors to be handled at multiple levels
Each exception handler processes only errors of
particular type (and its child types)
Other types of errors are processed by some other handlers later
Unhandled exceptions cause error messages
Try-finally ensures given code block is always
executed (even when an exception is thrown)
26
Editor's Notes
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
(c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*